Why when retrieving a string from the ResourceBundle.getBundle, incomprehensible characters

南楼画角 提交于 2019-12-20 05:58:36

问题


Please tell me how to solve this problem:

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();
        }

I'm trying to implement localization, I get the text from the created messages file, the problem is that instead of the necessary characters, it outputs "?????? ??????? ????" Googled, the problem seems to be with encodings, I tried to do it like this:

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

And also created 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;
    }

}

Nothing has happened... JSP has "UTF-8" encoding, files - default "ISO-8859-1"

来源:https://stackoverflow.com/questions/48473647/why-when-retrieving-a-string-from-the-resourcebundle-getbundle-incomprehensible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!