Unable to read js files from url with correct encoding… here is a solution to the problem.
Unable to read js files from url with correct encoding
I want to > from url Read the js file as a string
I
tried several methods (read from url or download and then read), but I keep getting unreadable characters such as ( _ s d :’ ………. I . .
What I’ve tried:
1. Download the file from URL:
FileUtils.copyURLToFile(jsUrl, file);
2。 Read from URL:
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(jsUrl);
read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
sb.append(line).append("\n");
}
in.close();
} catch (Exception e) {
}
return sb.toString();
If I manually download the file from the url (page -> save as…) – it can be opened with normal UTF-8 encoded Notepad++.
Can anyone help me with this strange file?
Solution
It is GZIP compressed. Use GZIPInputStream
.
Update
InputStream stream = url.openStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
Update 2
Use URLConnection:
URLConnection cnt = url.openConnection();
InputStream stream = cnt.getInputStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
BufferedReader read = new BufferedReader(new InputStreamReader(stream, "UTF-8"));