雄猫保存文件给出异常



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

   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());

雄猫总是给我一个例外:

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.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.

检查uploads.getAbsolutePath()以查看它是否将相对路径映射到预期的文件夹。如果是,请查看运行 tomcat 的帐户是否具有对该文件夹的读/写访问权限。

最新更新