0

java.io.IOException: java.io.FileNotFoundException: G:\uploads (Access is denied) 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 "";
}
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
ujjwol shrestha
  • 145
  • 1
  • 12
  • 2
    The implied question: " why am I getting this error?" I'm guessing that Utils.UPLOAD_DIRECTORY is "g:\uploads" in which case the server is not running with high enough permissions to write to that directory. – Mark Sholund Nov 06 '15 at 19:18
  • Possible duplicate of [FileNotFoundException when using FileWriter](http://stackoverflow.com/questions/14596861/filenotfoundexception-when-using-filewriter) – Mark Sholund Nov 06 '15 at 19:20
  • You can try with a java program, a simple main java programa that write a file to that directory. You have to run the program with the same user that you run de appserver. – reos Nov 06 '15 at 20:11
  • @Thevenin Thanx mate.!!! Finally it did worked. I had 2 input type file where I only uploaded 1 file but after I upload both the file it did worked. – ujjwol shrestha Nov 07 '15 at 17:11

1 Answers1

-1

In my case, when I was sending multiple file upload but when some files are missing it throws FileNotFoundException (Access Denied). So I added required attributes to all upload fields and it works. Files uploaded successfully.