简体   繁体   English

如何在 Java 中将标准输出更改为“UTF-8”

[英]How can I change the Standard Out to "UTF-8" in Java

I download a file from a website using a Java program and the header looks like below我使用 Java 程序从网站下载文件,标题如下所示

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

There is no encoding specified没有指定编码

What I do is after downloading I pass the name of the file to another application for further processing.我所做的是下载后将文件名传递给另一个应用程序以进行进一步处理。 I use我用

System.out.println(filename);

In the standard out the string is printed as Textk³rzung.asc在标准输出中,字符串被打印为Textk³rzung.asc

How can I change the Standard Out to "UTF-8" in Java?如何在 Java 中将标准输出更改为“UTF-8”?

I tried to encode to "UTF-8" and the content is still the same我尝试编码为“UTF-8”,内容还是一样

Update:更新:

I was able to fix this without any code change.我能够在没有任何代码更改的情况下解决这个问题。 In the place where I call this my jar file from the other application, i did the following在我从其他应用程序调用它的 jar 文件的地方,我执行了以下操作

java -DFile.Encoding=UTF-8 -jar ....

This seem to have fixed the issue这似乎解决了这个问题

thank you all for your support谢谢大家的支持

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. 您看到的结果表明您的控制台希望文本使用Windows“代码页850”编码-字符ü具有Unicode代码点U + 00FC。 The byte value 0xFC renders in Windows code page 850 as ³. 字节值0xFC在Windows代码页850中呈现为³。 So if you want the name to appear correctly on the console then you need to print it using the encoding "Cp850": 因此,如果您希望该名称正确显示在控制台上,则需要使用“ 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. 这是否是您的“其他应用程序”所期望的是另一个问题-如果其他应用程序也正在读取其标准输入为Cp850,则它将仅看到正确的名称。

The default encoding of System.out is the operating system default. System.out的默认编码是操作系统默认值。 On international versions of Windows this is usually the windows-1252 codepage. 在国际版本的Windows上,这通常是Windows-1252代码页。 If you're running your code on the command line, that is also the encoding the terminal expects, so special characters are displayed correctly. 如果您在命令行上运行代码,这也是终端所期望的编码,那么特殊字符将正确显示。 But if you are running the code some other way, or sending the output to a file or another program, it might be expecting a different encoding. 但是,如果您以其他方式运行代码,或将输出发送到文件或其他程序,则可能期望使用不同的编码。 In your case, apparently, UTF-8. 对于您而言,显然是UTF-8。

You can actually change the encoding of System.out by replacing it: 实际上,您可以通过替换System.out来更改其编码:

try {
    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    throw new InternalError("VM does not support mandatory encoding UTF-8");
}

This works for cases where using a new PrintStream is not an option, for instance because the output is coming from library code which you cannot change, and where you have no control over system properties, or where changing the default encoding of all files is not appropriate. 这适用于无法使用新PrintStream情况,例如,因为输出来自无法更改的库代码,无法控制系统属性,或者无法更改所有文件的默认编码的情况适当。

Try to use: 尝试使用:

 PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(test);

在尝试其他答案后,你的问题的这个答案https://stackoverflow.com/a/42957623/11628646对我来说就像一个魅力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM