简体   繁体   中英

Create file under linux Tomcat with unicode characters in filename

Very simple code:

try {
     new File("/home/user/programm/log/тест1.log").createNewFile();
} catch (Exception e) {
     System.out.println(e);
}

When I run this as simple java app, it works well, but when I run this under Tomcat, this results in ????1.log file name.

Environement:

  • Ubuntu 14.04
  • Tomcat 8
  • Java 8

What I already tried: run tomcat with options:

  • -Dfile.encoding=UTF-8
  • -Dsun.jnu.encoding=UTF-8

My locale env looks like this:

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Some investigation :

After debug into File.createNewFile method, I found that native method java.io.UnixFileSystem.createFileExclusively invokes String.getBytes method with argument charsetName=ANSI_X3.4-1968. This encoding was default before I redefined them by file.encoding and sun.jnu.encoding. I can see UTF-8 for these variables through JVisualVm.

Environment variable LC_ALL should be defined. In my case I set it as LC_ALL=en_US.UTF-8 . I started Tomcat with upstart script and java didn't see enverinment variables outside of this script. After I added this env to tomcat.conf problem was fixed.

env LC_ALL=en_US.UTF-8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM