4

I am a new bie, i want files to get downloaded when user clicks on download option its opening in the browser instead of download option like save as/open.Here i referred for the same and every where they have suggested to use

Response.AddHeader("Content-disposition", "attachment; filename=" + Name);

But i don't know where and how to use. Actually i'm getting the url value from query written which return url as one of the object of bean stored in arraylist(This list is having other values also with url ). I am having the url values inside the arraylist as bean as like

type=.pdf
release date=12/3/08
name=hai.pdf
url=/files/en/soft/doc/docs/hai.pdf

I am getting this array list in my controller like this

ArrayList details = dao.getdetails(Bean.getNumber());

and pass this into view like this

Map.put("details", details);
modelView.setViewName("details_list");
modelView.addAllObjects(Map);
return modelView;

in jsp i have iterated this array list and diplays the content like this

Type    name            Release Date            
.txt    hai.pdf     May 21st 2012   Download

.txt    hello.txt   May 21st 2012   Download

For download i have used like this in jsp

<td colspan="2" valign="top">                           
<a href="${details.Url}"/>
<img src="/images/download.gif" alt="Download" border="0" align="right"></a>
</td>

here on click of download its opening in browser.I need this to be downloaded instead. Please help me in how to use or handle

response.setHeader("Content-Disposition", "attachment;");

where to add the above for my requirement or if i can do with any java script also.Please help me in solving the above.

anto
  • 315
  • 3
  • 9
  • 18

3 Answers3

5

Here is one way of doing it:

  1. Create a web Filter (or this way )
  2. Map this filter to the PDF URL.
  3. In the doFilter() method, set the response header for content download.

Example :

public void doFilter(ServletRequest request, 
   ServletResponse response, FilterChain chain)
   throws IOException, ServletException {

          String name = request.getAttribute("filename");

           response.addHeader("Content-disposition", "attachment; filename=" + name);
           chain.doFilter(request, response);


}

You can set the filename as an request attribute (reqest.setAttribute()) from your controller class

Filters are pretty standard in Java web stack.

Santosh
  • 17,667
  • 4
  • 54
  • 79
0

It's depend on the header which browser get in response.

Suppose if header is image/png then browser will show it. Same way if you send same image with application/octet-stream then browser will force to download it.

take a look http://en.wikipedia.org/wiki/Byte_stream

In a project I have figure out that sending request from browser are different.

If you upload a image from Firefox or IE then it's will upload them as image/png wherever chrome upload them as application/octet-stream.

ankit
  • 87
  • 2
  • 12
  • 1
    can you please suggest according to my requirement. – anto Jun 11 '12 at 05:40
  • I am not sure how you can implement it in Java. If you change the Content-Type in Response to application/octet-stream then browser will surely force to download it. see I found another question in SO http://stackoverflow.com/questions/186867/what-content-type-to-force-download-of-text-response – ankit Jun 11 '12 at 05:46
  • Please any one can help me according to my requirement.I know that i need to use response.setHeader("Content-Disposition", "attachment;filename=_blank_"); but according to my requirement.Please any one can read my post and help.give me exactly where i need to add and what i need to add – anto Jun 11 '12 at 14:00
-1

just try to add header

response.setHeader("Content-Type: application/force-download");

Naresh
  • 785
  • 1
  • 11
  • 23
  • :Thanks but i dont know where to add? i f add this in controller its shows the whole page as download option i want only on click of download it should navigate to download – anto Jun 11 '12 at 05:17