You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by GitBox <gi...@apache.org> on 2020/05/27 03:38:44 UTC

[GitHub] [tomcat] xiaofanTo commented on pull request #291: fixed-chinese-encoding

xiaofanTo commented on pull request #291:
URL: https://github.com/apache/tomcat/pull/291#issuecomment-633821731


   ok , all info in this .
   tomcat version : 8.5.55
   java version : 1.8.0_181-b13
   os version : Mac OS X 10.15.4
   
   and i  debug this problem this morning .
   I found the final cause of this problem is because of the **PropertyResourceBundle** class .
   
   My test code is as follows :
   `
   public class Test {
       public static void main(String[] args) throws IOException {
           //1. inputstream
           InputStream inputStream = Test.class.getResourceAsStream("/test.properties");
           PropertyResourceBundle prb1 = new PropertyResourceBundle(inputStream);
           String title1 = prb1.getString("title");
           System.out.println(title1);
   
           //2. reader
           InputStreamReader reader = new InputStreamReader(Test.class.getResourceAsStream("/test.properties"),"UTF-8");
           PropertyResourceBundle prb2 = new PropertyResourceBundle(reader);
           String title2 = prb2.getString("title");
           System.out.println(title2);
       }
   }
   `
   the test.properties contens is : 
   `title=测试`
   
   When you initialize him, the input is an InputStream , then Properties will load this stream,The garbled problem arises here . 
   ` public PropertyResourceBundle (InputStream stream) throws IOException {
           Properties properties = new Properties();
           properties.load(stream);
           lookup = new HashMap(properties);
       }
   `
   
   So I think you should replace the above with the following initialization method. 
   ` public PropertyResourceBundle (Reader reader) throws IOException {
           Properties properties = new Properties();
           properties.load(reader);
           lookup = new HashMap(properties);
       }`
   
   when you use this reader , you can set the charset to **utf-8** 
   
    


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org