Servlet上传java.io.FileNotFoundException:

时间:2012-01-21 20:25:47

标签: java apache-commons

我正在尝试使用servlet创建上传表单,到目前为止,我有以下内容;

    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: 我做错了什么?

ERROR:

    /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)

2 个答案:

答案 0 :(得分:2)

我的猜测是你要写文件的目录是写保护的,或者你没有权限在那里写。您不应该将这些文件写在此目录中,因为如果取消部署或重新部署Web应用程序,它将被删除。考虑一下Tomcat的目录是由Tomcat负责的。

选择要在其中存储上传内容的Tomcat之外的目录,并使用包含此路径的参数配置您的webapp。在代码中使用此参数来获取路径并写入上传的文件。

答案 1 :(得分:1)

请阅读http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/disk/DiskFileItemFactory.html上的文档:

  

如果未另行配置,则默认配置值为   如下:

     
      
  • 尺寸阈值为10KB。
  •   
  • Repository是系统默认临时目录,由System.getProperty(“java.io.tmpdir”)返回。
  •   

您需要查看System.getProperty("java.io.tmpdir"),而不是当前正在使用的当前工作目录。

否则,可能更喜欢使用重载的DiskFileItemFactory构造函数来明确指定存储文件的位置。

JB的答案同样有效。即使您更正了我在此处提到的内容,您仍然需要确保您运行Web应用程序的进程/用户对您选择的任何目录具有适当的权限。