How to Avoid garbled file names in the Code for compiling file downloads in Java

Source: Internet
Author: User

When you are working on a project, you often need to compile a File Download module. If the file name is English, it is very simple and there will be no garbled characters. However, if a text similar to Chinese or other characters appears, the file name will be garbled. So how can we avoid it? We need to convert the file name before the code downloaded from the file.

 

In IE, we need to use the URLEncoder class for conversion. The Code is as follows:

Filename = URLEncoder. encode (filename, "UTF-8 ");

 

If you use this method, it is difficult to use it in the Firefox browser. The following code should be used in Firefox:

Filename = new String (filename. getBytes ("UTF-8"), "ISO8859-1 ");

 

So if we want to write a common code, we need to identify the browser before conversion.

 

If (request. getHeader ("User-Agent "). toLowerCase (). indexOf ("firefox")> 0) {<br/> filename = new String (filename. getBytes ("UTF-8"), "ISO8859-1"); <br/>} else {<br/> filename = URLEncoder. encode (filename, "UTF-8"); <br/>}

 

In fact, the Code is not complex and the problem is easily solved.

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.