java.nio.file.InvalidPathException:格式错误的输入或使用本地字符的输入包含不可映射的字符 [英] java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters when using national characters

查看:3641
本文介绍了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.<init>(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)

发生的代码如下:

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!");
        }
    }

我搜索了可能的解决方案,并且大多数建议将语言环境设置为UTF-8,所以我想如果将Linux中的语言环境设置为UTF-8可以解决此问题,但是我发现它已经是UTF了.一直都是-8,尽管进行了新设置,但我仍然遇到相同的问题.

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=

在Windows 7上我没有这个问题,它完美地创建了目录,所以我想知道是否需要改进Java代码以更好地处理这种情况,或者是否需要在Linux中进行某些更改.

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.

我在其上运行的Linux是Raspberry Pi 2上的Raspbian:

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"

我在Tomcat 7服务器(我相信Java版本是1.8)上运行我的应用程序,我的setenv.sh开头为:export JAVA_OPTS="-Dfile.encoding=UTF-8 ...

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...

在我的Tomcat的setenv.sh开头添加了额外的选项Dsun.jnu.encoding = UTF-8后,重新启动了某些内容.

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

目前,我的setenv.sh开始看起来像这样

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.<init>(FileOutputStream.java:206)
        at java.io.FileOutputStream.<init>(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();

似乎在(new FileOutputStream(myOutputFile))上失败了;尝试使用File对象初始化FileOutputStream时,该对象具有从字符串创建的路径,该字符串是从上述异常中的路径检索到的,并在末尾添加了文件名.

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.

在没有国家符号的情况下在其中创建路径和文件的效果与更改setenv.sh之前的效果一样完美,因此看来问题出在路径中仍然与国家符号有关……

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...

推荐答案

如果在源代码中对国家字符进行了硬编码,请将源文件转换为相同的编码.您可以使用vim:

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<Enter>
:w<Enter>

如果有问题,您会收到一条消息(不可映射的字符(...)").

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

对我来说,这个问题与1.编码错误的硬编码字符或2.在将路径传递给方法的过程中以某种方式丢失编码有关.

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.

这篇关于java.nio.file.InvalidPathException:格式错误的输入或使用本地字符的输入包含不可映射的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆