为什么从ResourceBundle.getBundle中检索字符串时难以理解的字符

时间:2018-01-27 07:32:25

标签: java encoding localization internationalization

请告诉我如何解决这个问题:

Locale locale = new Locale(language);
        ResourceBundle messages = ResourceBundle.getBundle("i18n.messages", locale, utf8Control);
        try {
            String message = new String(messages.getString(key).getBytes("ISO-8859-1"), "UTF-8");
            pageContext.getOut().write(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

我正在尝试实现本地化,我从创建的消息文件中获取文本,问题是,而不是必要的字符,它输出“?????? ??????? ??? ?” 谷歌搜索,问题似乎是编码,我试着这样做:

String message = new String(messages.getString(key).getBytes("ISO-8859-1"), "UTF-8")

还创建了utf8Control:

public class Utf8Control extends ResourceBundle.Control {

    @Override
    public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException {

        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, "properties");
        ResourceBundle bundle = null;
        InputStream stream = null;
        if (reload) {
            URL url = loader.getResource(resourceName);
            if (url != null) {
                URLConnection connection = url.openConnection();
                if (connection != null) {
                    connection.setUseCaches(false);
                    stream = connection.getInputStream();
                }
            }
        } else {
            stream = loader.getResourceAsStream(resourceName);
        }
        if (stream != null) {
            try {
                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        }
        return bundle;
    }

}

什么都没发生...... JSP有“UTF-8”编码,文件 - 默认为“ISO-8859-1”

0 个答案:

没有答案