Ilkar
Ilkar

Reputation: 2177

openConnection get xml with UTF-8

I want to download and save xml file using openConnection.

The problem is, that when i save file, there is wrong charset.

My code is:

URL url = new URL(partnersEntity.getUrl());

            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Content-Length", "500000");
            urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
            urlConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");


            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            Timestamp currentTimestamp = new Timestamp(now.getTime());

            File file = new File(myFile);

            FileWriter writer = new FileWriter(file);

            IOUtils.copy(urlConnection.getInputStream(), writer);
            writer.close();

After that in my file i see marks like "??" in special charters place.

What should i change ?

Upvotes: 0

Views: 2679

Answers (2)

jtahlborn
jtahlborn

Reputation: 53694

Don't use a Reader/Writer in this situation, leave the xml as-is.

        FileOutputStream out = new FileOutputStream(file);

        IOUtils.copy(urlConnection.getInputStream(), out);

by using FileWriter, you are writing the xml data using the platform default charset, which is never what you want to do. always treat xml as binary data, not textual data.

Upvotes: 2

AgilePro
AgilePro

Reputation: 5598

(1) Check that the resource you are reading really is in UTF-8. You requested using the 'Accept-Charset' but that does not guarantee. However. Let's assume that it is UTF-8.

(2) Specify the character set of the writer you are using. By using FileWriter, you get the 'default' character set of the environment you are running in ... might not be UTF-8. Better to be specific using OutputStreamWriter.

OutputStream os = new OutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");

(3) Tell the copy how to interpret the incoming stream:

IOUtils.copy(urlConnection.getInputStream(), writer, "UTF-8");

Upvotes: 1

Related Questions