• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Return a filestream w/ Spring MVC

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm currently working on a Spring MVC app and I was wondering how I could generate files in memory and make those files downloadable for the end user?
I've seen solutions setting the contentType to "application/octetstream" but I don't know how and where I would do that in an MVC app... Or maybe I should reroute those urls generating files dynamically to a regular request handler instead of a controller?

A little bit of "best practice" advice would be greatly appreciated.

Thank you
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to do this recently also, but I dont have the code at hand. Will see if I can dig it up tomorrow

Essentially you are correct about setting the 'contentType' on the HttpServletResponse. I believe you would need to do this on one of your controller methods. Which version of Spring are you using?

Cheers,
Rosco
 
yann luppo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rosco Duncan wrote:I had to do this recently also, but I dont have the code at hand. Will see if I can dig it up tomorrow

Essentially you are correct about setting the 'contentType' on the HttpServletResponse. I believe you would need to do this on one of your controller methods. Which version of Spring are you using?

Cheers,
Rosco



Thanks for your reply Rosco, our POM points to Spring MVC 2.5.4.
 
Rosco Duncan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello mate,

Here is some code similar to what I used some time ago. The key is getting a handle on the actual HttpServletResponse and interacting with it directly. From memory there are a bunch of things that you can do with the ContentType and the reponse headers to indicate to the http client (ie web browser) what to do with the data stream. Also note you probably will have a completely different way of generating the data content to what I have done. The key bits are how you interact with the response and later the outputStream.

Good luck,

Rosco

 
yann luppo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rosco you da man! That looks exactly like what I need. I'm gonna give that a shot today. Thanks man!
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic