Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidPathException for chinese filename

I am trying to copy a file on an ubuntu machine using the java statement

Files.copy(new File("/tmp/source/测试.xlsx").toPath(), new File("/tmp/dest/测试.xlsx").toPath(), StandardCopyOption.REPLACE_EXISTING);

But i get the following error

java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters: /tmp/source/测试.xlsx at sun.nio.fs.UnixPath.encode(UnixPath.java:147) ~[na:1.8.0_91] at sun.nio.fs.UnixPath.(UnixPath.java:71) ~[na:1.8.0_91] at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:281) ~[na:1.8.0_91] at java.io.File.toPath(File.java:2234) ~[na:1.8.0_91]

This runs perfectly fine when i run it on my eclipse. Also, the code works fine with all english characters.

like image 236
user3820369 Avatar asked May 24 '16 09:05

user3820369


1 Answers

This could be a JDK Bug

Set the following system properties sun.jnu.encoding=UTF-8 and file.encoding=UTF-8.

  • Check this to add system properties in tomcat maven plugin.
  • Use the -D option if you are running a java program in command line. (-Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8)
like image 121
Mad Piranha Avatar answered Sep 28 '22 08:09

Mad Piranha