2013-10-29 24 views
0

寫入文件我已經上被指示覆制在本地映射的網絡目錄中的文件,例如N:\some\directory\file.txt的Windows上的Tomcat 7.0.42此Web應用程序,但它沒能做到所以。的Tomcat Web應用程序不能在網絡

定義輸出文件,我使用的URI語法file:///n:/some/directory/file.txt,但兩者Files.copyFileUtils.copyFile拋出IOException一些不那麼有用的錯誤信息:

URI desturi = new URI(srcpath); 
File dest = new File(desturi); 
Files.copy(source.toPath(), dest.toPath()); 
// error message: "c:\local\dir\file.txt -> n:\some\directory\file.txt" 

FileUtils.copyFile(source, dest); 
// error message: "Destination 'N:\some\directory' directory cannot be created" 

一些額外的信息:

  • 我當然可以讀取並在該目錄上寫
  • 一個可執行的.jar可以複製FIL e沒有問題
  • 如果目標位於本地驅動器,一切正常
  • 我不認爲安全管理器已加載,但我該如何檢查它?
  • 在Tomcat啓動與我用來登錄同一用戶(例如user.name是一樣的USERNAME環境變量)

我的想法......

更新片段的堆棧痕跡。對於Files.copy

java.nio.file.NoSuchFileException: c:\local\dir\file.txt -> n:\some\directory\file.txt 
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) 
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) 
    at sun.nio.fs.WindowsFileCopy.copy(Unknown Source) 
    at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source) 
    at java.nio.file.Files.copy(Unknown Source) 
    at it.augea.print.server.MyClass.copyFileToPath(MyClass.java:886) 
    at it.augea.print.server.MyServlet.doPost(MyServlet.java:126) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

FileUtils.copyFile

java.io.IOException: Destination 'N:\some\directory' directory cannot be created 
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1015) 
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:968) 
    at it.augea.print.server.MyClass.copyFileToPath(MyClass.java:886) 
    at it.augea.print.server.MyServlet.doPost(MyServlet.java:126) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

阿帕奇Commons.io是V2.2,順便說一句。查看源代碼,這些是涉及的行:

File parentFile = destFile.getParentFile(); 
if (parentFile != null) 
    if (!parentFile.mkdirs() && !parentFile.isDirectory()) 
     throw new IOException("Destination '" + parentFile + "' directory cannot be created"); 
+0

你可以通過提及絕對路徑複製文件? – Jhanvi

+1

請使用堆棧跟蹤發佈這些不太有用的錯誤消息 – WeMakeSoftware

+0

@Jhanvi I * am *使用絕對路徑。更重要的是,規範的路徑。我不是嗎? – MaxArt

回答

0

JVM無法爲要複製的文件創建文件夾。

檢查,如果JVM訪問它。

+0

它的確如此。如果這是JVM的問題,那麼.jar可執行文件也會失敗。 – MaxArt

+0

是與可執行jar相同的用戶運行的tomcat嗎? – WeMakeSoftware

+0

是的。這是在Windows中登錄的用戶。 – MaxArt

0

當Tomcat(或任何其他程序)運行,因爲它在一個服務帳戶下運行一個Windows服務和那些帳戶不 - 默認 - 訪問映射驅動器(或者,如果沒有記錯的話任何網絡路徑)。 Tomcat無法看到N:驅動器,因此任何嘗試寫入它的操作都將失敗。

有幾種可能的解決方案。我的建議是爲Tomcat創建一個域帳戶,該帳戶具有最低的必要權限,以便a)在運行的計算機上作爲服務運行,b)在網絡上寫入所需的權限。最後,使用UNC路徑// machinename/sharename/path/in/share而不是映射驅動器,你應該很好。

+0

我目前無法檢查您的答案,因此您可能是對的。無論如何,Tomcat的啓動方式與我用於登錄的Windows用戶相同,並且在打開網絡文件時從來沒有任何問題:它有什麼區別? – MaxArt

+0

是的。即使它是相同的用戶,它也不會看到您的映射驅動器。如果你使用UNC路徑,它應該工作。 –