Java – processing the file name * parameter with spaces through RFC 5987, resulting in “in the file name”

I have some legacy code I'm working on (so I can't just use the URL with the encoded file name component), allowing users to download files from our website Since our file names usually have many different languages, they are stored as UTF-8 I wrote some code to handle the RFC 5987 conversion to the correct filename * parameter This is very useful until I have a file name with non ASCII characters and spaces According to the RFC, the space character is not attr_ Char, so it is encoded as I have new versions of chrome and Firefox, and they are converted to download I tried not to encode spaces and put the encoded file name in quotation marks and got the same result I've sniffed the response from the server to verify that the servlet container doesn't mess up my titles and that they look right to me The RFC even includes examples What did I miss, or did all these browsers have errors related to this?

Thank you in advance The code I use to encode file names is as follows

Peter

public static boolean bcsrch(final char[] chars,final char c) {
    final int len = chars.length;
    int base = 0;
    int last = len - 1; /* Last element in table */
    int p;

    while (last >= base) {
        p = base + ((last - base) >> 1);

        if (c == chars[p])
            return true; /* Key found */
        else if (c < chars[p])
            last = p - 1;
        else
            base = p + 1;
    }

    return false; /* Key not found */
}

public static String rfc5987_encode(final String s) {
    final int len = s.length();
    final StringBuilder sb = new StringBuilder(len << 1);
    final char[] digits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    final char[] attr_char = {'!','#','$','&','\'','+','-','.','0','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','^','_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','|','~'};
    for (int i = 0; i < len; ++i) {
        final char c = s.charAt(i);
        if (bcsrch(attr_char,c))
            sb.append(c);
        else {
            final char[] encoded = {'%',0};
            encoded[1] = digits[0x0f & (c >>> 4)];
            encoded[2] = digits[c & 0x0f];
            sb.append(encoded);
        }
    }

    return sb.toString();
}

to update

The following is a screenshot of the download dialog box for Chinese character files with spaces mentioned in my comment

Solution

As Julian pointed out in his comments, I made a novice Java error and forgot my character to byte conversion (so I encoded the code point of the character rather than the byte representation of the character), so the encoding was completely incorrect This is explicitly mentioned in RFC 5987 I will publish the correction code for the conversion Once the encoding is correct, the browser will correctly recognize the filename * parameter and the file name used for download is correct

The following is the corrected escape code, which operates on the UTF - 8 bytes of the string The file name that caused me trouble is now correctly encoded as follows:

Content disposal: attachment; Filename * = UTF-8 "museum museum jpg

public static String rfc5987_encode(final String s) throws UnsupportedEncodingException {
    final byte[] s_bytes = s.getBytes("UTF-8");
    final int len = s_bytes.length;
    final StringBuilder sb = new StringBuilder(len << 1);
    final char[] digits = {'0','F'};
    final byte[] attr_char = {'!','`','~'};
    for (int i = 0; i < len; ++i) {
        final byte b = s_bytes[i];
        if (Arrays.binarySearch(attr_char,b) >= 0)
            sb.append((char) b);
        else {
            sb.append('%');
            sb.append(digits[0x0f & (b >>> 4)]);
            sb.append(digits[b & 0x0f]);
        }
    }

    return sb.toString();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>