我有一个 spring web 项目,现在应该使用 cxf 和 web 服务来实现。
一个功能是输出一个xml文件。我正在使用 XMLStreamWriter 来完成这项任务。一切正常。
但是当我将一些 cxf 依赖项添加到我的 POM 文件中时,输出 xml 文件获得“IBM1252”编码。之后无法读取 xml 文件。异常:抛出“无效的编码名称 IBM1252”。
我添加了以下依赖项:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
我没有更改我的代码中的任何内容。我什至试过这个:
XMLStreamWriter writer = facOut.createXMLStreamWriter(fileWriter);
writer.writeStartDocument("UTF-8", "1.0");
我仍然得到“IBM1252”编码。
有谁知道这可能是什么原因吗?
最佳答案
不要使用始终使用系统默认编码的 FileWriter,而是使用 OutputStreamWriter。
像这样
OutputStream os=null;
Writer fileWriter = null;
File f=new File("myfile");
try {
os =new FileOutputStream(f);
fileWriter =new OutputStreamWriter(os,"UTF-8");
} finally {
// close writer, then outputstream if they are not null
}
....
关于java - CXF、XMLStreamWriter 和编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371206/