我有一个Java程序,几乎可以完美运行。我正在Mac上进行开发,并正在将linux投入生产。当mac搜索文件系统并将新文件名插入数据库时,它工作得很好。但是,当我按下linux框并执行搜索/插入操作时,它会找到带有某些字符的文件,例如IE:BélaFleck。它们在数据库以及mac和linux文件系统中看起来与我相同。实际上,mac和linux盒已将NFS挂载到文件所在的第3个系统(linux)。
我已经转储了字节,可以看到linux和mac如何从文件系统BélaFleck中看到字符串。
Linux:
utf8bytes[0] = 0x42
utf8bytes[1] = 0x65
utf8bytes[2] = 0xcc
utf8bytes[3] = 0x81
utf8bytes[4] = 0x6c
utf8bytes[5] = 0x61
utf8bytes[6] = 0x20
utf8bytes[7] = 0x46
utf8bytes[8] = 0x6c
utf8bytes[9] = 0x65
utf8bytes[10] = 0x63
utf8bytes[11] = 0x6b
linux says LANG=en_US.UTF-8
苹果电脑:
utf8Bytes[0] = 0x42
utf8Bytes[1] = 0xc3
utf8Bytes[2] = 0xa9
utf8Bytes[3] = 0x6c
utf8Bytes[4] = 0x61
utf8Bytes[5] = 0x20
utf8Bytes[6] = 0x46
utf8Bytes[7] = 0x6c
utf8Bytes[8] = 0x65
utf8Bytes[9] = 0x63
utf8Bytes[10] = 0x6b
mac says LANG=en_US.UTF-8
尝试了这个,仍然没有喜悦。
java -Dfile.encoding=UTF-8
我正在使用java nio文件来获取目录:
java.nio.file.Path path = Paths.get("test");
然后走在路上
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
然后,由于这是测试路径中的子目录:
file.getParent().getName(1).toString()
任何人都对这里的故障以及如何解决这个问题有任何想法?
谢谢。
一些搜索显示OS X总是分解文件名:
这向我暗示您可能不小心切换了输出:第一个字节数组被分解了,所以我猜想它是从Mac提取的,而第二个是从Linux提取的。
无论如何,如果希望它们对于所有系统都相同,则可以自己进行分解:
String name = file.getParent().getName(1).toString();
name = Normalizer.normalize(name, Normalizer.Form.NFD);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句