java - 无法获取 UTF-8 形式的 URL 内容

标签 java url utf-8 inputstream

我正在尝试从 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/

相关文章:

java - 日期对象转换为 (mm/dd/yyyy) 中的日期对象

java - 如何将字符串拆分为整数?

javascript - 避免在浏览器上显示从 Spring @ResponseBody 返回的数据

python - 我如何去一个随机的网站? - Python

php - file_get_contents - URL 中的特殊字符 - 特例

windows - Windows 上的 NASM 在 UTF-8 源代码的第一行出现错误,无论它是什么 : hello. asm:1: 错误:解析器:需要指令

java - Java wait() 方法的行为

java - printDebugData(JTabble table) 的作用是什么?

javascript - Jquery - 拆分 URL 并将类添加到正文

Android比较UTF-8字符串与UTF-8输入字符串的edittext