无法以正确的编码从 url 读取 js 文件
Can't read js file from url in correct encoding
我想从 url https://d3c3cq33003psk.cloudfront.net/opentag-67008-473432.js
中读取 js 文件作为字符串
我尝试了几种方法(从 url 读取或下载然后读取),但一直收到无法读取的字符,例如 ��(��_��s�������� ��:`����.����i....
我尝试的方式:
1. 从 url:
下载文件
FileUtils.copyURLToFile(jsUrl, file);
2。阅读 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();
如果我从 url(页面-> 另存为...)手动下载文件 - 它可以使用普通 UTF-8 编码的 Notepad++ 打开。
谁能帮我处理这个奇怪的文件?
已压缩。使用 GZIPInputStream
.
更新
InputStream stream = url.openStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
更新 2
使用 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"));
我想从 url https://d3c3cq33003psk.cloudfront.net/opentag-67008-473432.js
中读取 js 文件作为字符串我尝试了几种方法(从 url 读取或下载然后读取),但一直收到无法读取的字符,例如 ��(��_��s�������� ��:`����.����i....
我尝试的方式:
1. 从 url:
FileUtils.copyURLToFile(jsUrl, file);
2。阅读 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();
如果我从 url(页面-> 另存为...)手动下载文件 - 它可以使用普通 UTF-8 编码的 Notepad++ 打开。
谁能帮我处理这个奇怪的文件?
已压缩。使用 GZIPInputStream
.
更新
InputStream stream = url.openStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
更新 2
使用 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"));