Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Servlet Interest »

Re: How to download file in Servlet?

Henry Reardon

2004-02-27


I used the following code to do something very similar to what you are
trying to do. 'fZip' is a File reference to the file I want to download. It
will normally download automatically but, if that fails, it creates a
download link that the user can click manually. This code works very well
for me.

/* Get the name of the zip file without any of the path information. */
 String strZipName = fZip.getName();

 out.println("<META http-equiv=\"Refresh\" content=\"2; URL=" +
request.getContextPath() + "/" + strZipName + "\">");
 out.println("</head>");
 out.println("<h1>Download Zip File</h1>");
 out.println("<p>In a few seconds, your browser should pop up a 'Save As'
dialog. If it does not, <a href=\"" + request.getContextPath() + "/" +
strZipName + "\">click here</a>.</p>");
 out.println("</body>");
 out.println("</html>");


Good luck,

Rhino

>From: Edward King <zhangsc@(protected)>
>Reply-To: "A mailing list for discussion about Sun Microsystem's Java
>      Servlet API Technology." <SERVLET-INTEREST@(protected)>
>To: SERVLET-INTEREST@(protected)
>Subject: How to download file in Servlet?
>Date: Fri, 27 Feb 2004 10:37:52 +0800
>
>I am trying to use a servlet to download an text file to the client from
>server,File located c:\1.txt in Server,when user explore this page,IE
>reminder user if download file "1.txt".I write following code.But when I
>explore this page,IE didn't remind me if download file and it didn't
>domnload file at all.How to correct following code to realize automatically
>download? What errors is in my code?
>
>public void doPost(HttpServletRequest request, HttpServletResponse
>response) throws ServletException, IOException {
>   response.setContentType("multipart/form-data");
>   String file_name=new String("c:\\1.txt");
>   String data=new String("data");
>
>response.setHeader("Content-Disposition","attachment;filename="+file_name);
>   PrintWriter out = response.getWriter();
>   out.println(file_name);
>}
>
>Thanks
>
>___________________________________________________________________________
>To unsubscribe, send email to listserv@(protected)
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca

___________________________________________________________________________
To unsubscribe, send email to listserv@(protected)
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html


©2008 junlu.com - Jax Systems, LLC, U.S.A.