Servletresponse Download File garbled

Source: Internet
Author: User

With the help of powerful search engines and local experimentation.

In general, there are two ways to solve garbled characters:

1. Encode the file name (including Chinese characters) into UTF-8

String downloadorigalfile = "test file garbled. xlsx";

String downloadfilename = Java.net.URLEncoder.encode (Downloadorigalfile, "UTF-8");

Response.setheader ("Content-disposition", "attachment; Filename= "+ downloadfilename);

If a space appears in the file name, you need to replace the space with%20,utf-8 + to indicate a space

DownloadFileName = Downloadfilename.replace ("+", "%20");

2. Encode the file name into Iso-8859-1 (because the default encoding is iso-8859-1)

String downloadorigalfile = "test file garbled. xlsx";

Final String locale_encoding = "gb2312";

Here's locale_encoding=gb2312: You can usually use Java's system.getproperties () traversal to get

Generally there are two values may be related to this, please refer to the combination of actual use.

Sun.jnu.encoding=gbk

File.encoding=utf-8

Final String default_encoding = "Iso-8859-1";

String downloadfilename = new String (Downloadorigalfile.getbytes (locale_encoding), default_encoding);

If a space appears in the file name, you need to replace the space with%20

DownloadFileName = Downloadfilename.replace ("", "%20");

Here GetBytes () If you do not specify the encoding, there may be problems when cross-platform, it is recommended to explicitly specify the encoding.

Response.setheader ("Content-disposition", "attachment; Filename= "+ downloadfilename);

====

3, if you want to display Chinese in the browser address, you generally need to modify the default encoding of the Web container uriencoding= "UTF-8"

(i.e. download files directly using get)

For example, a Tomcat container that joins in the following location in the Tomcat_home/conf/server.xml

<connector port= "8080" protocol= "http/1.1" uriencoding= "UTF-8"

connectiontimeout= "20000"

redirectport= "8443"/>

4. Special codes commonly used in URLs

+ the + sign in the URL indicates a space%2b

Spaces in a space URL can be encoded with the + number or%20

/delimited directories and subdirectories%2f

? Separating the actual URLs and parameters%3f

% Specifies special characters%25

# indicates bookmark%23

& the delimiter between the parameters specified in the URL%26

= The value of the specified parameter in the URL%3d

Reference Link:

The All-Evil company code is a snapshot, so I'm very sorry for the reference to you ...


This article is from "Silent Rain" blog, please be sure to keep this source http://longjubing.blog.51cto.com/2669571/1759361

Servletresponse Download File garbled

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.