JSP download TXT and Excel two kinds of files

Source: Internet
Author: User

1Download TXT File This took me a lot of time the reason is that when I download txt file with IE, I open the file directly in the page. Although looked up some information, also looked at other people's solution, but can not solve the problem, and finally found that is a letter to blame: less write a letter hehe enough sloppy!!! The code is as follows: OutputStream o=Response.getoutputstream (); byteb[]=New byte[500]; File Fileload=NewFile ("E:/test.txt"); Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment; Filename=text.txt "); LongFilelength=fileload.length (); String length1=string.valueof (filelength); Response.setheader ("Content_length", length1); FileInputStream in=NewFileInputStream (fileload); intN;  while((N=in.read (b))!=-1) {O.write (b,0, N);    } in.close ();    Out.clear (); out=pagecontext.pushbody ();2the only difference between downloading an Excel file and downloading a TXT file is that the ContentType value is set differently: OutputStream o=Response.getoutputstream (); byteb[]=New byte[500]; File Fileload=NewFile ("E:/text.xls");        Response.reset (); Response.setcontenttype ("Application/vnd.ms-excel"); Response.setheader ("Content-disposition", "attachment; Filename=text.xls "); LongFilelength=fileload.length (); String length1=string.valueof (filelength); Response.setheader ("Content_length", length1); FileInputStream in=NewFileInputStream (fileload); intN;  while((N=in.read (b))!=-1) {O.write (b,0, N);    } in.close ();    Out.clear (); out=Pagecontext.pushbody ();        These two would have been put together, because my page needs to determine whether to download the TXT file or xls file here to separate the need to note that the last two sentences must be added, otherwise it will appear getoutputstream () Error!!!! Example:<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%> <%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > function Download (filename) {var URL= encodeURI ("down2.jsp?filename=" +filename); Window.location.href=URL; }     </script> General test:<a href= "javascript:void (0)" onclick= "Download (' a.txt ');" >down a.txt</a>Chinese file name test:<a href= "javascript:void (0)" onclick= "Download (' Chinese. txt ');" >down Chinese .txt</a>General Steering:<a href= "A.txt" >down.txt</a> </body> down2.jsp:<%@ page language= "Java"Import= "java.util.*,java.io.*" pageencoding= "UTF-8"%> <%request.setcharacterencoding ("Utf-8"); String filename= Request.getparameter ("filename"); FileName=NewString (Filename.getbytes ("iso-8859-1"), "UTF-8"); SYSTEM.OUT.PRINTLN (filename); OutputStream o=Response.getoutputstream ();byteb[]=New byte[500]; /*** Get the current path of the file *@paramargs*/String Serverpath=request.getrealpath ("\ \"); File Fileload=NewFile (serverpath+filename); Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment; Filename= "+filename); LongFilelength=fileload.length (); String length1=string.valueof (filelength); Response.setheader ("Content_length", length1); FileInputStream in=NewFileInputStream (fileload);intN; while((N=in.read (b))!=-1) {O.write (b,0, N); } in.close (); Out.clear (); out=pagecontext.pushbody ();%>

Reprint: http://blog.csdn.net/z69183787/article/details/12995451

JSP download TXT and Excel two kinds of files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.