我正在编写一个运行在Tomcat上的web应用程序。
在这个web应用程序中,我想创建/保存如下文件:
File destFile = new File(path + "/" + fileName);
destFile.createNewFile();现在棘手的是fileName是一个UTF8编码的字符串。不幸的是,所有的UTF-8字符,如德语元音,都被替换为"?“在实际创建的文件名中。
因此,fileName "hellö.txt“以"hell?.txt”结尾。
我该如何解决这个问题呢?
我在Ubuntu 14.04服务器上运行,据我所知,一切都设置为支持UTF-8。
我已经在JAVA_OPTS中添加了-Dfile.encoding=UTF8,但这并没有帮助。
干杯。
发布于 2017-04-11 04:23:37
解决了!
在我的例子中,它不是一个Java bug。取而代之的是,我服务器上的区域设置被破坏了。在像这里描述的那样修复这个问题之后,https://askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue现在甚至可以与java.io.File一起工作了。
发布于 2017-04-11 02:51:50
即使有可能,我也不确定,在文件名中包含像'ö','ä‘这样的字符是非常糟糕的做法。相反,您应该将它们替换为它们的同义词“oe”或“ae”。
我为“德语元音”写了一个基本函数
string removeBadCharacters(String string) {
string = string.replace("ö", "oe");
string = string.replace("ä", "ae");
string = string.replace("ü", "ue");
string = string.replace("ß", "ss");
return string;
}然后,您只需传递您的fileName并使用格式化字符串:
File destFile = new File(path + "/" + removeBadCharacters(fileName));https://stackoverflow.com/questions/43330734
复制相似问题