JSP page Download file

Source: Internet
Author: User
Tags save file

Convert the created Excel document to a stream that requires output: either the file is banished to the hard disk or the output stream is output to the browser for download. File stream: FileOutputStream

1         New FileOutputStream ("F://workbook.xls");         Workbook.write (FOS); 3 Fos.close ();   

Output stream: Response.getoutputstream ()

      Response.reset (); 2         response.setcontenttype ("APPLICATION/VND.MS-EXCEL;CHARSET=GBK");  3         response.setheader ("Content-disposition", "Attachment;filename=account.xls");  4 OutputStream out = response.getoutputstream (), workbook.write (out);       

Tip:

*response.reset (); Clear the header blank line
* GetResponse's Getwriter () method outputs the stream to the page twice in a row, and the second stream includes the first stream,
* So you can use the Response.reset or Resetbuffer method.
* The difference between the Resetbuffer method and the Reset method is that the header and status code are not cleared.

If you find that the browser side does not pop up "save file as" to select the path window after this setting, check whether the preceding code appears:

1.response.setcontenttype ("Text/html;charset=utf-8");//Set encoding format
2.PrintWriter out = Response.getwriter (), resulting in an inability to determine the output stream

3, Excel file name is Chinese when garbled or unknown file type error, consider using Urlencoder to transcode the file name

1         String name = Java.net.URLEncoder.encode (FileName, "Utf-8");  2         response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8");  3         response.setheader ("Content-disposition", "Attachment;filename=" +name.tostring () + ". xls"4 outputstream out = response.getoutputstream (), workbook.write (out);        

  

JSP page Download file

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.