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.