【问题标题】:How to store resource file in struts in UTF-8 format如何以UTF-8格式将资源文件存储在struts中
【发布时间】:2014-07-26 05:59:08
【问题描述】:

有没有办法以 UTF-8 格式将资源文件(.properties)存储在 struts 1.x 中。每当我保存文件时,它都会以 ISO-8859 格式保存。

另外,我可以在 struts 目录的资源文件夹中创建一个文本文件,该文件将用作属性文件(以便文本文件可以保存为 UTF-8 格式),并在我的应用程序中读取该文件以显示静态希腊语、俄语或任何语言的文本。

我已阅读以下内容并尝试提出解决方案,但没有奏效:

https://stackoverflow.com/a/863854/3493471

使用这个我的代码看起来像这样:

import java.util.Properties;
import java.io.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertiesListener {

        private static Log log = LogFactory.getLog(PropertiesListener.class);

        protected  static Properties readProperties(String name){
                Properties prop = new Properties();
                InputStream inputStream = null;
                try {
                        //inputStream = new FileInputStream(name);
                        inputStream =  EditMailboxAction.class.getResourceAsStream(name);
                        Reader reader = new InputStreamReader(inputStream, "UTF-8");
                        log.debug("Length :: "+reader.ready());
                        try {
                                prop.load(reader);
                            } catch(Exception ex){
                                 log.debug("1111111111111111111111111");
                                ex.printStackTrace();
                             }finally {
                                reader.close();
                             }
                }catch(FileNotFoundException ex){
                         log.debug("22222222222222222222");
                        ex.printStackTrace();
                  }catch(Exception ex){
                        ex.printStackTrace();
                        }
                         finally {
                  }

                return prop;
        }
}

我已经调用了上面的函数来读取文件:

Properties properties = PropertiesListener.readProperties("myProperties.properties");

但这并没有帮助。

由于 myProperties.properties 不存储 UTF-8 字符,我不明白如何

Reader reader = new InputStreamReader(inputStream, "UTF-8");

将有助于阅读 UTF-8 字符。

【问题讨论】:

  • “但这并没有帮助”并没有描述您所看到的内容。您谈论“每当我保存文件时”-您如何保存文件?在编辑器中?使用 Java 工具?
  • 我的应用程序在 Linux 中运行,但是为了安全起见,我将 myProperties.properties 文件 ftp 到 windows,在 notepad++ 中打开它并输入 utf8 字符并以编码 UTF-8 保存文件。然后我将文件ftp到linux。不幸的是,当我在 linux 中使用 vim 打开文件时,甚至当我将其 ftp 回 Windows 并再次在 notepad++ 中打开时,它会将输出显示为乱码。 (注意:我已经将 putty 设置更改为使用 utf-8)
  • 这听起来只是在FTP中使用了不正确的模式。您可能希望以二进制模式使用 FTP。但从根本上说,如果您不能适当地传输和编辑文件,这与 Java 无关。
  • 没关系,现在 application.properties 文件显示 UTF8 字符。但是当它与 struts-bean 属性一起使用时,它会显示垃圾字符: 有关更多信息,请参阅此问题:@987654322 @

标签: java utf-8 struts-1 properties-file struts-config


【解决方案1】:

搞定了。我已经修改了 struts-config.xml 并且可以正常工作。

【讨论】:

  • 在回答您自己的问题时:您至少应该提供您所做工作的详细信息。你看,截至目前,答案对其他人几乎毫无用处。因此,您应该要么完全删除问题,要么提供真正帮助其他未来读者的信息。 “我解决了”对任何人都没有帮助。
  • 你是如何修改struts-config.xml的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多