File Download: "Content-disposition", "attachment; Filename= Chinese name >>> solution

Source: Internet
Author: User

Excerpt from: http://blog.csdn.net/wangzhi_821/article/details/5047777

File download often appears the following problems: Response.setheader ("Content-disposition", "attachment; Filename=" +as+ ");
If the as directly in the Chinese name, it will be directly on the IE page open Excel file (pop-up will open in the IE page opens, note at this time, "Do you want to open or save this file?" ", the name will show a similar cak .... The name of the implementation is not the normal *.xls format name):

Instead of popping up and opening the Excel file separately. The solution is: (Program fragment)

String as = "I see. xls";
String fileName = as;//= Java.net.URLEncoder.encode (AS, "UTF-8");
/* Depending on the locale of the request, the Chinese operating system is usually gb2312 */
FileName = new String (as.getbytes ("GB2312"), "iso_8859_1");

as = FileName;
Response.setcontenttype ("application/vnd.ms-excel");
Response.setheader ("Content-disposition", "attachment; Filename= "+ AS
+ "");

Bufferedinputstream bis = null;
Bufferedoutputstream BOS = NULL;
try {
bis = new Bufferedinputstream (new
FileInputStream (Getservletcontext (). Getrealpath ("" + filename));
bis = new Bufferedinputstream (new FileInputStream (Dirfile));
BOS = new Bufferedoutputstream (Response.getoutputstream ());

byte[] buff = new byte[2048];
int bytesread;

while ( -1! = (bytesread = bis.read (buff, 0, buff.length))) {
Bos.write (Buff, 0, bytesread);
}
} catch (Final IOException e) {
System.out.println ("appears ioexception." + e);
} finally {
if (bis! = null)
Bis.close ();
if (BOS! = NULL)
Bos.close ();
}

The most critical sentence in the program section:

New String (As.getbytes ("GB2312"), "iso_8859_1");

Rename the file to transcode. Ok the result is normal >>>

This can be downloaded with the Chinese name, and is another open Excel file.

File Download: "Content-disposition", "attachment; Filename= Chinese name >>> solution

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.