Java localized filenames

前端 未结 4 1261
轮回少年
轮回少年 2021-02-09 14:05

How can i set localized filenames in java.Currently everytime i click on a localized file having a non-ascii filename in my application, the windows save dialog box pops out, bu

4条回答
  •  感情败类
    2021-02-09 14:37

    Although it's old question, it's still actual. I found solution that works for all my browsers.

    See my post in other thread:
    Java servlet download filename special characters

    In short, browsers expect that value in filename parameter is encoded in browsers native encoding (if no different charset is specified for filename parameter). Browser's native encoding is usually utf-8 (FF, Opera, Chrome), but for IE it is win-1250. Hence, if we put value into filename parametr, that is encoded by utf-8/win-1250 according to user's browser, it should work.

    For example, if we have file named omáčka.xml,
    for FireFox, Opera and Chrome I response this header (encoded in utf-8):

    Content-Disposition: attachment; filename="omáčka.xml"
    

    and for IE I response this header (encoded in win-1250):

    Content-Disposition: attachment; filename="omáèka.jpg"
    

    Java example is in my post that is mentioned above.

    Note #1 (@dkarp):

    Be aware of using URLEncoder.encode() since this method doesn't encode input string into url-encoding. This method encodes input strings into form-encoding which is very similiar but differs in some cases - for example space character ' ' is encoded as '+' instead of '%20'.

    To perform correct url encoding you should rather use URI class:

    URI uri = new URI(null, null, "foo-ä-€.html", null);
    System.out.println(uri.toASCIIString());
    

提交回复
热议问题