2015-04-21 98 views
4

我目前正在嘗試上傳多個文件,並且正在將文件正確上傳到目標目錄中。但在日誌中它給我這個錯誤:FileNotFoundException(是一個目錄)

java.io.FileNotFoundException: /home/sandeep/java_proj/jee_tut/files (Is a directory) 

at java.io.FileOutputStream.open(Native Method) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:221) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:171) 
at com.jee.controller.FileProcessing.uploadFiles(FileProcessing.java:35) 
at com.jee.controller.View.doPost(View.java:48) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Thread.java:745) 

這是我的代碼上傳文件:

final String UPLOAD_DIR="files"; 

    String applicationPath="/home/sandeep/java_proj/jee_tut"; 
    String uploadDirPath= applicationPath + File.separator + UPLOAD_DIR; 

    try{ 
     File fileDir=new File(uploadDirPath); 
     if(!fileDir.exists()){ 
      fileDir.mkdirs(); 
     } 
     InputStream inputStream= null; 
     OutputStream outputStream = null; 
     try{ 
      for(Part part:request.getParts()){ 
       String fileName=getFileName(part); 

        File outputPath=new File(uploadDirPath + File.separator + fileName); 
        inputStream = part.getInputStream(); 
        outputStream = new FileOutputStream(outputPath); 

        int read = 0; 
        final byte[] bytes = new byte[1024]; 
        while((read=inputStream.read(bytes)) != -1){ 
         outputStream.write(bytes, 0, read); 
        } 
       System.out.println(fileName); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
      return ""; 
     } 
     finally{ 
      if(inputStream != null){ 
       inputStream.close(); 
      } 
      if(outputStream != null){ 
       outputStream.close(); 
      } 
     } 
     return uploadDirPath; 

    }catch(Exception e){ 
     e.printStackTrace(); 
     return ""; 
    } 

由於我是新來的Java,我不知道爲什麼它給我這個錯誤。 如何調試?

+0

論'線35 FileProcessing.java'你想開一個'FileOutputStream'到一個目錄。你不能那樣做。 – Kayaman

+1

您在2015年使用java.io.File的任何原因?如果是Java 7+,請改爲使用java.nio.file。 – fge

+0

@fge:謝謝你的擡頭。我將開始使用Java 7+ –

回答

7

看起來像在某些情況下filename是空的或空的,所以File outputPath=new File(uploadDirPath + File.separator + fileName);將是一個目錄,這裏new FileOutputStream(outputPath);你試圖寫入目錄而不是文件。所以你應該檢查是否filename不是空白。

+0

謝謝@Jens。這正是問題所在。我解決了它。 –

+0

我也想指出看起來像一個文件的路徑,實際上可能是一個目錄。例如,如果通過調用文件上的.mkdirs()而不是其父目錄,則「home/test/file.png」可能是目錄中的錯誤。 – TheIT

5

您的代碼中有很多錯誤。例如,您只需關閉最後的InputStream s和OutputStream s;你應該關閉它們中的每一個。你在這裏做事的方式,你有資源泄漏。

二,這是2015年;因此請刪除File並改爲使用java.nio.file;另外,使用try-with-resources。

第三:現在你上傳的項目目錄;當應用程序在服務器上運行時,不要這樣做,這顯然不起作用。

樣品:

private static final Path BASEDIR = Paths.get("/path/to/upload/directory"); 

// in the upload method: 

Files.createDirectories(BASEDIR); 

String fileName; 
Path path; 

for (final Part part: request.getParts()) { 
    fileName = getFileName(part); 

    if (fileName.isEmpty()) 
     continue; 

    path = BASEDIR.resolve(fileName); 

    try (
     final InputStream in = part.getInputStream(); 
    ) { 
     Files.copy(in, path, StandardOpenOption.CREATE_NEW); 
    } 
} 
+0

這非常有幫助。謝謝 :) –

相關問題