Spring MVC Multipart文件随机上传FileNotFoundException

时间:2015-10-13 08:05:49

标签: spring-mvc filenotfoundexception multipart

我使用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

2 个答案:

答案 0 :(得分:1)

由于这个错误,我刚刚度过了一个恐怖的夜晚。我发现java.util.concurrent.Future类只能识别public String callLongRunOpperation() throws Exception { final long EXECUTION_TIMEOUT = 8L; java.util.concurrent.Future <Object> future; java.util.concurrent.ExecutorService executorService = java.util.concurrent.Executors.newFixedThreadPool(1); final String res; future = executorService.submit(new java.util.concurrent.Callable<Object>() { public Object call() throws Exception { return longrunApiCall(); } }); //execute try { res = (String) future.get(EXECUTION_TIMEOUT, java.util.concurrent.TimeUnit.SECONDS); } catch (java.util.concurrent.ExecutionException ex) { ex.printStackTrace(); // handle execution exception here } catch (InterruptedException ee){ ee.printStackTrace(); // handle interrupt here } catch (java.util.concurrent.TimeoutException te){ // handle timeout here te.printStackTrace(); } finally { future.cancel(true); executorService.shutdown(); } return res; } 类。因此,如果将其传递给不是控制器的另一个bean,Spring将无法为您提供帮助。 MultiPartFile紧密地绑定到前屏幕(从浏览器到系统的通信-控制器是浏览器的入口点),这在某种程度上是有意义的。因此,任何对话都必须在Controller中在那里进行。

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

@Controller

答案 1 :(得分:0)

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

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

fileToImport是MultipartFile。