【问题标题】:Spring MVC Multipart file upload random FileNotFoundExceptionSpring MVC Multipart 文件上传随机 FileNotFoundException
【发布时间】:2015-10-13 08:05:49
【问题描述】:

我使用 Spring MVC 构建了一个 Web 应用程序,除了在文件上传中出现随机 FileNotFoundExceptions 之外,一切正常。我在网上找到了一些解决方案,例如使用不同的 tmp 文件夹,但我不断收到随机错误。

我的代码是:

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") final MultipartFile multipartFile,
                                             @RequestHeader("email") final String email, @RequestHeader("password") String password){
    if (authenticateUser(email, password)) {
        if (!multipartFile.isEmpty()) {
            System.out.println("Start processing");
            Thread thread = new Thread(){
                public void run(){
                    ProcessCSV obj = new ProcessCSV();
                    try {
                        File file = multipartToFile(multipartFile);
                        if(file !=null) {
                            obj.extractEvents(file, email, cluster, session);
                        }
                        else {
                            System.out.println("null File");
                        }
                    } catch (IOException e) {
                        System.out.println("File conversion error");
                        e.printStackTrace();
                    }
                }
            };
            thread.start();

            return "true";
        } else {
            return "false";
        }
    }
    else {
        return "false";
    }
}

和:

public File multipartToFile(MultipartFile multipartFile) throws IOException {

    File uploadFile = null;

    if(multipartFile != null && multipartFile.getSize() > 0) {
        uploadFile = new File("/tmp/" + multipartFile.getOriginalFilename());
        FileOutputStream fos = null;
        try {
            uploadFile.createNewFile();
            fos = new FileOutputStream(uploadFile);
            IOUtils.copy(multipartFile.getInputStream(), fos);
        } catch (FileNotFoundException e) {
            System.out.println("File conversion error");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("File conversion error");
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    System.out.println("File conversion error");
                    e.printStackTrace();
                }
            }
        }
    }
    else {
        System.out.println("null MultipartFile");
    }

    return uploadFile;
}

以及配置文件:

multipart.maxFileSize: 100MB
multipart.maxRequestSize: 100MB
multipart.location = ${user.home}
server.port = 8090

我使用了不同版本的 multipartToFile 函数,其中一个使用的是multipartfile.transferTo(),但我得到了相同的随机错误。有什么建议吗?

谢谢

编辑堆栈跟踪:

java.io.IOException: java.io.FileNotFoundException: /Users/aaa/upload_07720775_4b37_4b86_b370_40280388f3a4_00000003.tmp (No such file or directory)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:121)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:260)
    at main.RESTController.multipartToFile(RESTController.java:358)
    at main.RESTController$1.run(RESTController.java:241)
Caused by: java.io.FileNotFoundException: /Users/aaa/upload_07720775_4b37_4b86_b370_40280388f3a4_00000003.tmp (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:392)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:119)
    ... 3 more

【问题讨论】:

    标签: spring-mvc filenotfoundexception multipart


    【解决方案1】:

    我刚刚为这个错误度过了一个恐怖的夜晚。我发现MultiPartFile 只能被@Controller 类识别。因此,如果您将它传递给另一个不是控制器的 bean,Spring 将无法为您提供帮助。 @Controller 与前屏幕紧密绑定在某种程度上是有道理的(从浏览器到系统的通信 - 控制器是浏览器的入口点)。因此,任何对话都必须在 Controller 中进行。

    就我而言,我做了以下事情:

    @Controller
    public class FileUploadingController{
    
    
     @PostMapping("/uploadHistoricData")
     public String saveUploadedDataFromBrowser(@RequestParam("file") MultipartFile file) {
    
    try {
    
    
      String pathToFile = "/home/username/destination/"
    
      new File(pathToFile).mkdir();
    
      File newFile = new File(pathToFile + "/uploadedFile.csv");
    
      file.transferTo(newFile); //transfer the uploaded file data to a java.io.File which can be passed between layers
    
      dataService.processUploadedFile( newFile);
    } catch (IOException e) {
      //handle your exception here please
    }
    
    return "redirect:/index?successfulDataUpload";
     }
    
    }`
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,看起来 MultipartFile 在内部使用了不同的当前目录,所以所有非绝对路径都不起作用。 我必须将我的路径转换为绝对路径,然后它才能工作。 它也在 @RestController 和其他 bean 中工作。

      Path path = Paths.get(filename).toAbsolutePath();
      fileToImport.transferTo(path.toFile());
      

      fileToImport 是 MultipartFile。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 2014-04-19
        • 1970-01-01
        • 2014-12-27
        • 1970-01-01
        相关资源
        最近更新 更多