2012-01-21 130 views
3

我想創建一個使用servlet的上傳表單,到目前爲止,我有以下內容;Servlet上傳java.io.FileNotFoundException:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 

    if (isMultipart) { 
     FileItemFactory factory = new DiskFileItemFactory(); 
     ServletFileUpload upload = new ServletFileUpload(factory); 

     try { 
      List items = upload.parseRequest(request); 
      Iterator iterator = items.iterator(); 
      while (iterator.hasNext()) { 
       FileItem item = (FileItem) iterator.next(); 

       if (!item.isFormField()) { 
        String fileName = item.getName(); 

        String root = getServletContext().getRealPath(""); 
        File path = new File(root + "/uploads"); 

        if (!path.exists()) { 
         boolean status = path.mkdirs(); 

        } 

        File uploadedFile = new File(path + "/" + fileName); 
        System.out.println(uploadedFile.getAbsolutePath()); 
        item.write(uploadedFile); 

       } 
      } 

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

這似乎將文件上傳罰款,但它總是拋出一個java.io.FileNotFoundException: 難道我做錯了什麼?

錯誤:

/Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads/texsxxdddst.xml 
/Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads 
java.io.FileNotFoundException: /Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads (No such file or directory) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:194) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:145) 
    at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:426) 
    at servlets.uploadServlet.doPost(uploadServlet.java:48) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:680) 
+0

你能發佈一些關於異常的更多細節嗎? – Thomas

+0

哪個文件引發錯誤?你有沒有檢查過你要結束的文件名? –

+0

更新了異常 – Lunar

回答

1

我的猜測是,你想寫文件的目錄是寫保護的,或者你沒有寫權限。您不應將這些文件寫入此目錄,因爲如果取消部署或重新部署Web應用程序,它將被刪除。考慮Tomcat的目錄是由Tomcat負責的。

在Tomcat之外選擇一個您想存儲上傳的目錄,然後使用包含此路徑的參數配置您的webapp。在您的代碼中使用此參數來獲取路徑並寫入上傳的文件。

+0

我明白了,但是我看到很多例子,使用類似c:/ tmp的東西,但是如果你沒有c:/ – Lunar

+0

那麼會發生什麼,選擇另一條路徑。你明白我的答案嗎?這個想法是通過將其外部化(對web.xml中的參數,或屬性文件,系統屬性或數據庫中的設置)來使路徑可配置,無論如何。 –

+0

是的,我明白了,我已經試過這個,並且確保權限是正確的,但仍然給出錯誤 – Lunar

1

http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/disk/DiskFileItemFactory.html閱讀文檔:

If not otherwise configured, the default configuration values are as follows:

  • Size threshold is 10KB.
  • Repository is the system default temp directory, as returned by System.getProperty("java.io.tmpdir").

你需要看對System.getProperty("java.io.tmpdir"),不是你目前正在使用當前工作目錄。

否則,也可能優選使用重載的構造函數來明確指定文件存儲的位置。

JB的回答同樣有效。即使您更正了我在此提到的內容,仍然需要確保正在運行Web應用程序的進程/用戶對您選擇的任何目錄擁有適當的權限。

+0

我已經使用覆蓋,並確保我可以寫,但仍然有例外。 – Lunar