使用Java中的servlet上传图像时java.io.FileNotFoundException:G:\ uploads(拒绝访问)

时间:2015-11-06 19:05:29

标签: java servlets filenotfoundexception

  

java.io.IOException:java.io.FileNotFoundException:G:\ uploads(拒绝访问)       com.efashion.controller.ProductController.doPost(ProductController.java:52)       javax.servlet.http.HttpServlet.service(HttpServlet.java:644)       javax.servlet.http.HttpServlet.service(HttpServlet.java:725)       org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    for (Part part : request.getParts()) {
        String fileName = extractFileName(part);
        String fieldName = part.getName();
        if(fieldName.equalsIgnoreCase("file2")||fieldName.equalsIgnoreCase("file1")){
            part.write(Utils.UPLOAD_DIRECTORY + File.separator + fileName);

        }
        //part.write(fileName);
    }
    request.getRequestDispatcher("viewproduct.jsp").forward(request, response);
}

private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            System.out.println(s.substring(s.indexOf("=") + 2, s.length()-1));
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}

1 个答案:

答案 0 :(得分:-1)

在我的情况下,当我发送多个文件上传但缺少某些文件时,它将引发FileNotFoundException(访问被拒绝)。因此,我向所有上传字段添加了必需的属性,并且可以正常工作。文件已成功上传。