[英]Java 8: read files with special characters in name
我正在嘗試在Linux系統上讀取名稱中包含特殊字符的文件。 我對操作系統沒有任何控制權。
我嘗試使用IO和NIO。 我不斷
java.nio.file.InvalidPathException:格式錯誤的輸入或輸入包含不可映射的字符:/ mnt / Au?enr?ckspiegel,elektrisch verstellbar,1,版本-meta.xml
我無法真正在服務器中進行很多更改,我可以做些什么來解決我的應用程序中的問題?
System.out.println("Default Charset=" + Charset.defaultCharset()); // US_ASCII
這為我提供了帶有問號的特殊字符“ /mnt/Au?sen.xml”字符串
Files.list(Paths.get(path)).forEach(file -> {
log.info("file to string: " + file.toString());
String correctedFileName = "";
correctedFileName = new String(file.getFileName().toString().getBytes(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8);
log.info("corrected name: " + correctedFileName);
try {
ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(Paths.get(correctedFileName)));
ZipEntry entry = zipInputStream.getNextEntry();
while (entry != null) {
Path filePath = Paths.get(unzipLocation, entry.getName());
if (!entry.isDirectory()) {
unzipFiles(zipInputStream, filePath);
} else {
Files.createDirectories(filePath);
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
} catch (IOException e) {
e.printStackTrace();
}
});
您可以嘗試將文件名解析為URI來解決此類問題。
樣品:
File file = new File(path);
URI uri = file.toURI();
String asciiURI = uri.toASCIIString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.