Encoding issue on filename with Java 7 on OSX with jnlp/webstart

后端 未结 5 1415
孤街浪徒
孤街浪徒 2021-02-05 22:31

I have this problem that has been dropped on me, and have been a couple of days of unsuccessful searches and workaround attempts.

I have now an internal java swing prog

5条回答
  •  我寻月下人不归
    2021-02-05 22:44

    I take it it's acceptable to have maximal ASCII representation of the file name, which works in virtually any encoding.

    First, you want to use specifically NFKD, so that maximum information is retained in the ASCII form. For example, "2⁵" becomes "25"rather than just "2", "fi" becomes "fi" rather than "" etc once the non-ascii and non-control characters are filtered out.

    String str = "XXXYYY_è_ABCD/";
    str = Normalizer.normalize(str, Normalizer.Form.NFKD);
    str = str.replaceAll( "[^\\x20-\\x7E]", "");
    //The file name will be XXXYYY_e_ABCD no matter what system encoding
    

    You would then always pass filenames through this filter to get their filesystem name. You only lose is some uniqueness, I.E file asdé.txt is the same as asde.txt and in this system they cannot be differentiated.

提交回复
热议问题