Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1135

Downloading online text with different encodings

$
0
0

I am downloading online text, that can be uploaded by users, so texts can be UTF-8, ISO-8859-1, etc...

The problem is that I don't know which encoding are using the users, and if the user has uploaded a UTF-8 text it works perfect but if the user has uploaded a ISO-8859-1 text with accents (áé etc..) these characters are not shown correctly.

I tried to force text encoding to UTF-8 but it not works for all the cases (buffer.toString("UTF-8"))

This is my code:

javaUrl = new URL(URLParser.parse(textResource.getUrlStr()));                    connection = javaUrl.openConnection();                                          connection.setConnectTimeout(2000);                    connection.setReadTimeout(2000);                    InputStream input = new BufferedInputStream(connection.getInputStream());                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();                    int nRead;                    try{                                byte [] data = new byte [1024];                        while ((nRead = input.read(data, 0, data.length)) != -1) {                            buffer.write(data, 0, nRead);                        }                        buffer.flush();                        total = buffer.toString();                                      }finally{                        input.close();                        buffer.close();                    }

Viewing all articles
Browse latest Browse all 1135

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>