Download file set header filename to use iso8859-1 encoding reason __ encoding

Source: Internet
Author: User

In many cases, we write the program will be written to UTF-8 code, can be downloaded file set filename when it is counterintuitive, even set the encoding format for iso8859-1, the code is as follows (in English words do not need to deal with this): Java code Response.setheader ("Content-disposition", "attachment; Filename= "+ New String (" Chinese filename ". GetBytes (" Utf-8 ")," iso8859-1 "));

Extract the core point, Filename=new String ("Chinese filename". GetBytes ("Utf-8"), "iso8859-1");

First of all, why use iso8859-1 encoding, this is mainly due to the HTTP protocol, HTTP header requires its content must be iso8859-1 encoding, so we will eventually encode it as a iso8859-1 encoded string;

But why not use the "Chinese filename" directly in front of the GetBytes ("iso8859-1"); Such a code. Because ISO8859-1 encoded in the Code table, there is no Chinese characters, of course, can not pass the "Chinese filename". GetBytes ("Iso8859-1"), to get the correct "Chinese filename" in the Iso8859-1 encoding value, so again through the new String () to restore it is impossible to talk about. Therefore, the "Chinese filename". GetBytes ("Utf-8") is used to obtain its byte[] byte, which is encoded in bytes, that is, the new String ("Chinese filename". GetBytes ("Utf-8"), "iso8859-1") Re-compose it into a string and pass it to the browser.

Reference: http://jiapumin.iteye.com/blog/1006144

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.