0

When I try to upload a file through a form with the post method an error of the type NoSuchFileException occurs and i dont know why.

Here is the code:

if (req.getPart("file") != null) {
            Part file = req.getPart("file");
            String fileName = file.getSubmittedFileName();
            DiskFileItemFactory factory = new DiskFileItemFactory();
            File repository = new File("./uploads");
            factory.setRepository(repository);
            try {
                File thisFile = new File(repository, fileName);
                try (InputStream input = file.getInputStream()) {
                    Files.copy(input, thisFile.toPath());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            UserDao.getInstance().addPicture("./uploads" + fileName, oldUser);
            UserDao.getInstance().update(user, oldUser);
        }

I'm sure something is wrong, but I'm unable to see it...Could anyone help me??

At the begining i had this code, but trying to resolve the error i changed it:

 Part file = req.getPart("file");
    String filename = file.getSubmittedFileName();
    DiskFileItemFactory factory = new DiskFileItemFactory();
    File repository = new File("./uploads");
    factory.setRepository(repository);
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        List<FileItem> items = upload.parseRequest(req);

        File thisFile = new File(repository, filename);

        try (InputStream input = file.getInputStream()) {
            Files.copy(input, thisFile.toPath());
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }

Here is the StackTrace:

java.nio.file.NoSuchFileException: ./uploads/photo.jpeg
java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219)
java.base/java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:478)
java.base/java.nio.file.Files.newOutputStream(Files.java:223)
java.base/java.nio.file.Files.copy(Files.java:3143)
com.marcosvalens.controller.UserFormController.doPost(UserFormController.java:82)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
com.marcosvalens.interceptor.RequestLoginFilter.doFilter(RequestLoginFilter.java:39)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
AdamWist
  • 318
  • 2
  • 13

1 Answers1

0

You create a DiskFileItemFactory and then, apparently, you pray that it'll do something. That's not how you use this API; you must pass the factory variable to the constructor: new ServletFileUpload(factory). Your snippet does not contain this; presumably it'll be further up from what you pasted.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • at the beggining I have that but the error it also happen it..and it was never used...I put the old copy in the question in few seconds... – AdamWist Dec 17 '19 at 17:53