Response.setheader ("Content-disposition", "attachment; Filename= "+ filename+". xls "); file. Name is Chinese and is garbled

Source: Internet
Author: User
Tags knowledge base
Response.setheader ("Content-disposition", "attachment; Filename= "+ filename+". xls ");
If file. The name is Chinese and is garbled. The solution is
Method 1:
Response.setheader ("Content-disposition", "attachment; Filename= "+ (fileName," UTF-8 "));
Download the program has the above sentence, generally in the IE6 download prompt box will correctly display the name of the file, whether it is Simplified Chinese, or Japanese. However, if the text is more than 17 words, it cannot be downloaded.
First, through the original way, that is, using Urlencoder encoding, when the Chinese characters more than 17, IE6 cannot download the file. This is the bug of IE, see Microsoft Knowledge Base article KB816868. The reason may be that when IE handles the Response header, the length of the header is limited to about 150 bytes. and a Chinese character coding into a UTF-8 is 9 bytes, then 17 words is 153 bytes, so will be an error. And not with the suffix is wrong.
Method 2:
Response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"), " Iso8859-1 "));

In the case that the attachment file name is Simplified Chinese text, then this method is really the most effective, do not allow customers to upgrade ie. If Taiwan compatriots use it, they can change the gb2312 into Big5. But today's systems are often joined by the internationalization of support, generally using UTF-8. If you have simplified Chinese text in the file name, there is also traditional English, and Japanese. Then the garbled will be produced. In addition, on the Firefox (v1.0-en) download is garbled.

<%@ page import= "*"%>
<%@ page import= "java.util.*"%>
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>

String key = Request.getparameter ("key");
String title = Request.getparameter ("file");
if (title = = null) title= "download_table";
Title + = ". xls";
System.out.println ("Jilnjjhxjkhjhjhjhj" +title);
if (key! = null &&!key.equals (""))
Response.setcontenttype ("Application/x-msdownload");
Response.setheader ("Content-type", "application/x-msdownload");
Response.setheader ("accept-ranges", "bytes");
Response.setheader ("Content-disposition", "attachment; Filename=" + (title, "UTF-8"));
String str = (string) session.getattribute (key);
OutputStream OS = Response.getoutputstream ();
String Header = ""<meta http-equiv=\" content-type\ "content=\" Text/html;charset=gbk\ ">\n" +
"<meta name=progid content=excel.sheet>\n" +
"<style>\n" +
"<!--\ n" +
"A {\ n" +
"Font-size:9pt;\n" +
"Color:navy;\n" +
"Text-decoration:none;\n" +
"}\n" +
"A:hover {\ n" +
"Font-size:9pt;\n" +
"Color:darkorange;\n" +
"Text-decoration:underline;\n" +
"}\n" +
"-->\n" +
"</style>\n" +
Os.write (Header.getbytes ("GBK"));
Os.write (Str.getbytes ("GBK"));
Os.write ("</body>Os.flush ();
Os.close ();
Session.removeattribute (key);
Session.removeattribute (key);

* Function:
* Version 1.0
* Author:leo Leo.chou
* Create time:2006.06.01
* can copy and/or use and/or modify this program Free,but
* the segment above. Please mail me question, good day!
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">
<title></title><body bgcolor= "#F0FAFF" text= "#FF8040" marginwidth= "0" marginheight= "0" leftmargin= "5" topmargin= "1" >
<script language= "JavaScript" >
Alert ("Download tab error. ");

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: 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.