Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
242 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...