2017-06-03 111 views
0

在Linux(openSUSE)機器上,我試圖部署Tomcat 8應用程序(war文件),其中包含具有Unicode字符名稱的文件。Tomcat 8 Unicode文件名war文件部署問題

裏面的戰爭文件名的樣子:

бжк-природний-1496336830201.xml 

但經過部署的文件看起來像:

???-?????????????-1496336830201.xml 

如何告訴Tomcat的正確部署的文件名?

修訂

這裏面的Unicode文件名的示例war文件:war file

什麼是錯在這場戰爭中的文件的文件名?

修訂

我已經安裝了unzip-rcc,因爲它是在這裏建議https://superuser.com/questions/1215670/opensuse-unzip-unicode-issue現在解壓(控制檯命令)中的WAR文件工作正常,但Tomcat的部署仍然使用相同的問題的文件。

+0

如果解壓縮戰成目錄,並用特殊字符的文件名保存下來?另一個嘗試是確保JAVA具有正確的編碼。 –

+0

是的..解壓縮殺死文件名 – alexanoid

+0

這表明操作系統沒有處理這種編碼的字體或字符集。所以它不是Java錯誤:) –

回答

2

嘗試將這些設置在Tomcat啓動腳本:

export LC_ALL=en_US.UTF-8 
export LANG=en_US.UTF-8 
export LANGUAGE=en_US.UTF-8 

從經驗來看,Java將最多打印面朝下問號字符,它不知道如何編碼。

+0

非常感謝!我在這個問題上掙扎了幾天,現在隨着你的幫助它解決了!我已經將這些屬性添加到我的tomcat.conf文件中 – alexanoid

2

文件名確實是在zip .war文件中的UTF-8中。

try (ZipFile zipFile = new ZipFile(path, StandardCharsets.UTF_8)) { 
    zipFile.stream() 
     .forEachOrdered(entry -> System.out.printf("- %s%n", entry.getName())); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

但是zip沒有添加編碼(如bytes[] extra信息)。

三種解決方案可以考慮的是:

  • 一個短期的解決方案可能是一個UTF-8語言環境下運行Tomcat。
  • 最好的辦法是讓maven用UTF-8編碼構建一場戰爭。 (<onfiguration><encoding>UTF-8</encoding></configuration>
  • 通過轉換修復戰爭。

由於前兩種解決方案我沒有經驗。快速搜索沒有產生任何東西(「編碼」有點無處不在)。

修復代碼很簡單:

Path path = Paths.get(".../api.war").toAbsolutePath().normalize(); 
Path path2 = Paths.get(".../api2.war").toAbsolutePath().normalize(); 

URI uri = URI.create("jar:file://" + path.toString()); 
Map<String,String> env = new HashMap<String,String>(); 
env.put("create", "false"); 
env.put("encoding", "UTF-8"); 

URI uri2 = URI.create("jar:file://" + path2.toString()); 
Map<String,String> env2 = new HashMap<String,String>(); 
env2.put("create", "true"); 
env2.put("encoding", "UTF-8"); 

try (FileSystem zipFS = FileSystems.newFileSystem(uri, env); 
    FileSystem zipFS2 = FileSystems.newFileSystem(uri2, env2)) { 

    Files.walkFileTree(zipFS.getPath("/"), new SimpleFileVisitor<Path>() { 
     @Override 
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 
        throws IOException { 
      System.out.println("* File: " + file); 
      Path file2 = zipFS2.getPath(file.toString()); 
      Files.createDirectories(file2.getParent()); 
      Files.copy(file, file2); 
      return FileVisitResult.CONTINUE; 
     } 
    }); 

} catch(IOException e) { 
    e.printStackTrace(); 
}