4

I have a spring web project that should now be implemented with cxf and web services.

One function is to output an xml file. I am using the XMLStreamWriter for this task. Everything works fine.

BUT when I add some cxf dependencies into my POM-File, the output xml file gets the "IBM1252" encoding. The xml file can not be read afterwards. Exception: "Invalid encoding name IBM1252" is thrown.

I have added following dependencies:

    <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>

I didn't change anything in my code. And I have even tried this:

    XMLStreamWriter writer = facOut.createXMLStreamWriter(fileWriter);
    writer.writeStartDocument("UTF-8", "1.0");

I still get the "IBM1252" encoding.

Does anyone have an idea what could be the reason for it?

4

1 に答える 1

2

Instead of using FileWriter, which always uses your system default encoding, use OutputStreamWriter.

Like so

   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
}

....

于 2012-07-07T05:21:44.620 に答える