我正在尝试从 URL 读取内容,但它确实返回奇怪的符号,而不是“è”、“à”等。
这是我正在使用的代码:
public static String getPageContent(String _url) {
URL url;
InputStream is = null;
BufferedReader dis;
String line;
String text = "";
try {
url = new URL(_url);
is = url.openStream();
//This line should open the stream as UTF-8
dis = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = dis.readLine()) != null) {
text += line + "\n";
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
return text;
}
我看到了其他类似的问题,所有的答案都是这样的
Declare your inputstream as
new InputStreamReader(is, "UTF-8")
但我无法让它工作。
例如,如果我的网址内容包含
è uno dei più
我明白了
è uno dei più
我错过了什么?
最佳答案
从你的例子来看。您确实收到了多字节 UTF-8 字节流,但您的文本编辑器读入为 ISO-8859-1。告诉你的编辑器以 UTF-8 方式读取字节!
关于java - 无法获取 UTF-8 形式的 URL 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507785/