Does Servlet have some questions about the attachment of Chinese name when downloading?

Servlet technology needs to re-encode the file name when downloading Chinese name attachments
for example:

if (agent.contains("MSIE")) {
        // IE
        filename = URLEncoder.encode(filename, "utf-8");
        filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
        // 
BASE64Encoder base64Encoder = new BASE64Encoder();
        filename = "=?utf-8?B?"
                + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
        // 
        filename = URLEncoder.encode(filename, "utf-8");                
}

agentUser-Agent

uses this all the time, but there are a few questions that I don"t understand
1. Why use URLEncoder.encode (filename, "utf-8"); encode it in application/x-www-form-urlencoded MIME format? My entire web application environment is UTF-8, why should I use UTF-8 for coding?

2.application/x-www-form-urlencoded MIME format isn"t this the format that the client requests from the server? Why does the server respond in this format?

3. What kind of code can the browser parse? I see the use of new String (fileName1.getBytes ("UTF-8"), "ISO8859-1"); in Google, Firefox can also parse Chinese file names. Is there no way to tell the browser to decode according to a certain encoding?

Apr.19,2022

the main reason is that there is no standard for the Chinese file name of this attachment, and different browsers have different quirks, that is, they need to adapt one by one.

when using Content-Disposition headers, there is no single feasible way to encode non-ASCII names. browser compatibility is messy .

the syntax Content-Disposition that uses UTF-8 's is theoretically correct is very strange: ( filename*=UTF-8''foo%c3%a4 Yes, this is an asterisk with no quotation marks except for the empty single quotation marks in the middle)

this Header is a bit substandard ( HTTP / 1.1 specification acknowledges its existence, but does not require client support for it).

has a simple and very powerful alternative: use URL that contains the desired file name.

if the name after the last slash is the name you want, you don't need any additional Header!

this technique works:

/real_script.php/fake_filename.doc

if your server supports URL rewriting (for example, mod_rewrite is in Apache), you can hide the script part completely.

The character in

URL should be UTF-8, byte by byte urlencoded:

/mot%C3%B6rhead   -sharp motrhead

an example of a previous function

private String codedFileName(String userAgent, String filename, String encoding)  
        throws UnsupportedEncodingException {  
  
    String new_filename = URLEncoder.encode(filename, encoding);  
    // UAIEIE  
    String rtn = "=\"" + new_filename + "\"";  
    if (userAgent != null) {  
        userAgent = userAgent.toLowerCase();  
        // IEURLEncoder  
        if (userAgent.indexOf("msie") != -1) {  
            rtn = "=\"" + new_filename + "\"";  
        }  
        // Operafilename*  
        else if (userAgent.indexOf("opera") != -1) {  
            rtn = "*=UTF-8''" + new_filename;  
        }  
        // SafariISO  
        else if (userAgent.indexOf("safari") != -1) {  
            rtn = "=\""  
                    + new String(filename.getBytes(encoding), "ISO8859-1")  
                    + "\"";  
        }  
        // ChromeMimeUtilityISO  
        else if (userAgent.indexOf("applewebkit") != -1) {  
            new_filename = MimeUtility.encodeText(filename, "UTF8", "B");  
            rtn = "=\"" + new_filename + "\"";  
        }  
        // FireFoxMimeUtilityfilename*ISO  
        else if (userAgent.indexOf("mozilla") != -1) {  
            rtn = "*=UTF-8''" + new_filename;  
        }  
    }  
  
    return rtn;  
}  

reference:
https://stackoverflow.com/que...

Menu