在Windows上的JLabel中的UTF-8编码 [英] UTF-8 encoding in JLabel on Windows

查看:176
本文介绍了在Windows上的JLabel中的UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上的JLabel中有编码问题(在* nix操作系统上,一切都可以)。
以下是图片: http://i.imgur.com/DEkj3.png (有问题的字符是L在顶部,它应该是ł),这里的代码:

I have a problem with encoding in JLabel on Windows(on *nix OSes everything is okay). Here's an image: http://i.imgur.com/DEkj3.png (the problematic character is the L with ` on the top, it should be ł) and here the code:

public void run()
    {
            URL url;
            HttpURLConnection conn;
            BufferedReader rd;
            String line;
            String result = "";
            try {
                url = new URL(URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = rd.readLine()) != null) {
                    result += line;
                }
                rd.close();
            } catch (Exception e) {
                try
                {
                    throw e;
                }
                catch (Exception e1)
                {
                    Window.news.setText("");
                }
            }
                Window.news.setText(result);
        }

我试过 Window.news.setText新的String(result.getBytes(),UTF-8)); ,但没有帮助。也许我需要使用指定的JVM标志来运行我的应用程序?

I've tried Window.news.setText(new String(result.getBytes(), "UTF-8"));, but it hasn't helped. Maybe I need to run my application with specified JVM flags?

推荐答案

在数据到达窗口之前,使用新的InputStreamReader ,而不显示字符集。这将使用平台默认的字符集,这可能是Windows上的cp1252,因此你的坏字符。

You are breaking the data before it gets to the window when you use new InputStreamReader with no explicit charset. this will use the platform default charset, which is probably cp1252 on windows, hence your broken characters.

如果你知道你正在阅读的数据的字符集,你应该指定显式地,例如:

if you know the charset of the data you are reading, you should specify it explicitly, e.g.:

new InputStreamReader(conn.getInputStream(), "UTF-8")

在从任意URL下载数据的情况下,您应该更喜欢内容类型中的字符集标题,如果存在。

in the case of downloading data from an arbitrary url, however, you should probably be preferring the charset in the 'Content-Type' header, if present.

这篇关于在Windows上的JLabel中的UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆