java 输入地址格式错误,java.nio.file.InvalidPathException:格式错误的输入或使用本地字符的输入包含不可映射的字符...

I'm trying to create some directories which have national symbols like "äöü" etc. Unfortunately I'm getting this exception whenever that is being attempted:

java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters: /home/pi/myFolder/löwen

at sun.nio.fs.UnixPath.encode(UnixPath.java:147)

at sun.nio.fs.UnixPath.(UnixPath.java:71)

at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:281)

at java.nio.file.Paths.get(Paths.java:84)

at org.someone.something.file.PathManager.createPathIfNecessary(PathManager.java:161)

...

at java.lang.Thread.run(Thread.java:744)

My code where it occurs looks like this:

public static void createPathIfNecessary(String directoryPath) throws IOException {

Path path = Paths.get(directoryPath);

// if directory exists?

if (!Files.exists(path)) {

Files.createDirectories(path);

} else if (!Files.isDirectory(path)) {

throw new IOException("The path " + path + " is not a directory as expected!");

}

}

I searched for possible solutions and most suggest to set the locale to UTF-8, so I thought I would get this fixed if I set the locale in Linux to UTF-8, but I found out that it has already been UTF-8 all the time, and despite newly setting it, I'm still having the same problem.

$ locale

LANG=en_US.UTF-8

LANGUAGE=

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=

I'm not having this problem on Windows 7, it creates the directories perfectly, so I'm wondering whether I need to improve the java code to handle this situation better, or to change something in my Linux.

The Linux I'm running it on is a Raspbian on a Raspberry Pi 2:

$ cat /etc/*-release

PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"

NAME="Raspbian GNU/Linux"

VERSION_ID="7"

VERSION="7 (wheezy)"

ID=raspbian

ID_LIKE=debian

ANSI_COLOR="1;31"

HOME_URL="http://www.raspbian.org/"

SUPPORT_URL="http://www.raspbian.org/RaspbianForums"

BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

I am running my application on a Tomcat 7 Server (Java version is 1.8 I believe), my setenv.sh starts with: export JAVA_OPTS="-Dfile.encoding=UTF-8 ...

Does anybody have a solution to this problem? I need to be able to use those national symbols in directory/file names...

EDIT:

After adding the extra option Dsun.jnu.encoding=UTF-8 at the start of my setenv.sh for Tomcat and restarting something changed.

Currently the my start of setenv.sh looks like this

export JAVA_OPTS="-Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8

it seems like this exception is gone and the folder with the national symbols gets created, however the problem seems to not be solved completely, whenever I try to create/write to files within that directory, I now get:

java.io.FileNotFoundException: /home/pi/myFolder/löwen/Lowen.tmp (No such file or directory)

at java.io.FileOutputStream.open(Native Method)

at java.io.FileOutputStream.(FileOutputStream.java:206)

at java.io.FileOutputStream.(FileOutputStream.java:156)

at org.someone.something.MyFileWriter.downloadFiles(MyFileWriter.java:364)

...

at java.lang.Thread.run(Thread.java:744)

The code where it happens looks like this:

// output here

File myOutputFile = new File(filePath);

FileOutputStream out = (new FileOutputStream(myOutputFile));

out.write(bytes);

out.close();

It seems to fail on (new FileOutputStream(myOutputFile)); when it's trying to initialize the FileOutputStream with the File object, which has the path created from a string which was retrieved from the path in the exception above and an added filename at the end.

So now the directory is created, however writing or creating anything inside it still results in the exception above, although the file inside it doesn't event contain national symbols.

Creating paths and files in them when they have no national symbols works as perfectly as it did before the change in setenv.sh, so it looks like the problem is connected to the national symbols within the path still...

解决方案

If the national characters are hardcoded in your source, convert the source file to the same encoding. You can use vim:

vim SourceClassWithHardcodedCharacters.java

:set fileencoding=utf-8

:w

If there is an issue, you will get a message ("unmappable character (...)").

For me, the issue is related either with 1. hardcoding characters in incorrect encoding or 2. losing the encoding somehow during passing the path to the method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值