How to download file in Servlet? 2004-02-26 - By Edward King
Back 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) and include in the body 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <META content="MSHTML 6.00.2800.1400" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><FONT size=2>I am trying to use a servlet to download an text file to the client from server,</FONT><FONT size=2><FONT size=2>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?</FONT></FONT></DIV> <DIV><FONT size=2><FONT size=2></FONT> </DIV></FONT> <DIV><FONT size=2>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<BR> response.setContentType("multipart/form-data");<BR> String file_name=new String("c:\\1.txt");<BR> String data=new String("data");<BR> response.setHeader("Content-Disposition","attachment;filename="+file_name);<BR> PrintWriter out = response.getWriter();<BR> out.println(file_name);</FONT></DIV> <DIV><FONT size=2>}</FONT></DIV> <DIV><FONT size=2></FONT> </DIV> <DIV><FONT size=2>Thanks<BR></FONT><A href="mailto:SERVLET-INTEREST@(protected)"></A></DIV></BODY></HTML> __ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ __ To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff SERVLET-INTEREST". <p> 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 <p>
|
|