Java download file name garbled solution + file name garbled when uploading

Source: Internet
Author: User
Tags locale knowledge base
Response.setheader (...) Download Chinese filename garbled problem
Regarding the Chinese file downloading question, the on-line consultation and the answer questions already many, I originally processing downloads the code as follows:
Java code response.setheader ("Content-disposition", "attachment; Filename= "+ java.net.URLEncoder.encode (fileName," UTF-8 "));
Response.setheader ("Content-disposition", "attachment; Filename= "+ java.net.URLEncoder.encode (fileName," UTF-8 "));


Download the program has this sentence, generally in the IE6 download prompt box will correctly display the name of the file, whether it is Simplified Chinese, or Japanese. But did not carefully test the name of a very long filename in Chinese. Now after careful testing, found that the text as long as more than 17 words, you can not download. After a good Google and repeated testing, it is finally a systematic understanding of the problem, as follows:

I. Through my original way, that is, first with Urlencoder encoding, when the Chinese text more than 17, IE6 can not download files. This is the bug of IE, see Microsoft Knowledge Base article KB816868. The reason may be that the length of the header is limited to about 150 bytes when IE handles the Response header. and a encoding into UTF-8 is 9 bytes, then 17 words is 153 bytes, so it will be an error. Microsoft has provided a patch that can be downloaded from here. This patch needs to be installed IE6 SP1 first. Because I usually play the patch, my IE6 version number is 6.0.2800.1106.xpsp2_xxxxx. So I may have installed the patch so that it can be downloaded, but still the file name is truncated. Microsoft lets us wait for IE to release the next service pack. I also saw the good news on the internet today, under the pressure of Firefox, IE7 may release in the year. In addition, Firefox does not support such a way that the encoded%XX%XX will be displayed directly as a filename.


Two. I try to use the JavaMail Mimeutility.encode () method to encode the filename, which is encoded as =?gb2312? b?xxxxxxxx?= this form, and find the corresponding standard support from the RFC1522. Unfortunately, IE6 does not support this standard. I tried it, and Firefox was supported.

Three. A solution offered by many people on the Web: encoding file names into iso8859-1 appears to be a valid solution, with the following code:
Java code response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"), " Iso8859-1 "));
Response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"), " Iso8859-1 "));



In the case of ensuring that the attachment file name is in simplified text, then this method is indeed the most effective, do not allow customers to upgrade the IE. If Taiwan compatriots use, change gb2312 to Big5 on the line. But now the system is usually joined by the internationalization of support, universal use of UTF-8. If there is a simplified Chinese text in the file name, there is also traditional English, and Japanese. Then the garbled is produced. In addition, on my Computer Firefox (v1.0-en) download is garbled.

Compromise, I combined the one or three way, the code snippet is as follows:
Java Code String filename = urlencoder.encode (Atta.getfilename (),  "UTF-8");            /*           *  see http://support.microsoft.com/default.aspx?kbid=816868            */           if  (Filename.length ()  > 150)  {                string guesscharset = xxxx /* according to request's locale  to draw possible encoding, Chinese operating system is usually gb2312*/                fileName = new  String (Atta.getfilename (). GetBytes (Guesscharset),  "iso8859-1");             }           response.setheader (" Content-disposition ",   " Attachment; filename= " + filename);  
String fileName = Urlencoder.encode (Atta.getfilename (), "UTF-8");
         * * http://support.microsoft.com/default.aspx?kbid=816868 *
        /if (Filename.length () >) {
            String guesscharset = xxxx/* According to the locale of request, the Chinese operating system is usually gb2312*/
            fileName = new String (Atta.getfilename (). GetBytes (Guesscharset), "iso8859-1"); 
        }
        Response.setheader ("Content-disposition", "attachment; Filename= "+ fileName);




Firefox is not considered because it currently does not seem to have a strong intrusion into IE's corporate user market. It is often the progress, not the compatibility, that affects the customer's pay.

Typically, in a JSP page involving Chinese, the corresponding coding transformation is based on different situations:
To enable JSP pages to display Chinese correctly, add to the JSP page:
<%@ page contenttype= "text/html;charset=gb2312"%>
To enable a JSP page to properly receive a form with Chinese information submitted from the previous page, and to correctly read Chinese information from a database or file, you need to convert the resulting string to code such as:
String Str=new string (str.getbytes ("iso-8859-1"), "GBK");
When a JSP writes a Chinese character to a database or file, it is converted in the opposite way, such as:
String Str=new string (str.getbytes ("GBK"), "iso-8859-1");

PS: Here's "GBK" to be based on your JSP page charset content, not blindly copied.

Encoding conversion in JSP

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.