Tomcat 保存文件出现异常

Tomcat saving file gives exception

我正在尝试将上传的文件保存到我的磁盘,如下所示:

   Part filePart = req.getPart("pic");
    String fileName = filePart.getSubmittedFileName();
    InputStream fileContent = filePart.getInputStream();

    File uploads = new File("/images/gin");
    File file = new File(uploads, fileName);
    if(!file.exists())
        file.createNewFile();
    Files.copy(fileContent, file.toPath());

Tomcat 总是给我例外:

java.io.IOException: No such file or directory
    java.io.UnixFileSystem.createFileExclusively(Native Method)
    java.io.File.createNewFile(File.java:1006)
    com.springapp.mvc.servlets.AddItemServlet.doPost(AddItemServlet.java:39)
    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)

我不知道为什么它不起作用。有人可以帮助我吗?

检查 uploads.getAbsolutePath() 以查看它是否将相对路径映射到预期文件夹。如果是,请查看帐户 运行 tomcat 是否可以 read/write 访问该文件夹。

在创建新文件之前调用uploads.mkdirs();

 mkdirs()

 * Creates the directory named by this abstract pathname, including any
 * necessary but nonexistent parent directories.  Note that if this
 * operation fails it may have succeeded in creating some of the necessary
 * parent directories.