以UTF8格式URL从文本中获取数据

时间:2017-03-30 04:05:49

标签: java utf-8

服务器上有一个文本,纯文本但我尝试以UTF-8格式获取能够识别“ñ”等的数据。我试过这个

String readLine;
    try {
        URL url = new URL("http://miurl/data.txt");
        BufferedReader in = null;
        in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));

        while ((readLine = in.readLine()) != null) {
           byte ptext[] = readLine.getBytes(ISO_8859_1); 
           String value = new String(ptext, UTF_8); 
           System.out.println(value);
        }

    }
    catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

问题在于它从url读取数据但未编码为utf8,诸如“ñ”之类的字母被奇怪地打印出来。

2 个答案:

答案 0 :(得分:3)

从CSV文件中读取一些特殊字符时,我也遇到了同样的问题。 所以我使用下面的代码来读取文件

BufferedReader bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(userCsv), "ISO-8859-1"));

在您的情况下,您可以尝试

URL url = new URL("http://miurl/data.txt");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "ISO-8859-1"));

答案 1 :(得分:2)

你刚试过这个吗?

        String readLine;
        try {
            URL url = new URL("http://miurl/data.txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

            while ((readLine = in.readLine()) != null) {
                System.out.println(readline);
            }

        }
        catch (IOException ex) {
            ex.printStackTrace();
        }

我在here的示例文本上尝试了此操作 并且它可以很好地解析所有UTF-8字符。