How can I change the Standard Out to “UTF-8” in Java

前端 未结 3 1540
忘掉有多难
忘掉有多难 2020-12-20 22:49

I download a file from a website using a Java program and the header looks like below

Content-Disposition attachment;filename=\"Textkürzung.asc\";

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 23:35

    The result you're seeing suggests your console expects text to be in Windows "code page 850" encoding - the character ü has Unicode code point U+00FC. The byte value 0xFC renders in Windows code page 850 as ³. So if you want the name to appear correctly on the console then you need to print it using the encoding "Cp850":

    PrintWriter consoleOut = new PrintWriter(new OutputStreamWriter(System.out, "Cp850"));
    consoleOut.println(filename);
    

    Whether this is what your "other application" expects is a different question - the other app will only see the correct name if it is reading its standard input as Cp850 too.

提交回复
热议问题