简体   繁体   中英

MultiPartFile deleted too early

I'm using MultPartFile to upload a .zip from my front-end, sometimes this file is deleted before i do anything with it. I debbug the CommonsMultipartFile.class (intellij) to clean my ideas but i can't find nothing that explain why sometimes i can manipulate the MultPartFile and sometimes no.

In CommonsMultipartFile.class The File is deleted when he pass on isAvailable() method, and when isAvailable() try to validate if he exist, return false.

This is my code

 private void limparDiretorioCargasVXESalvarCarga(MultipartFile arquivoMultipartFile, String caminhoDiretorio, String nomeArquivo) throws Step1Exception {
    try {
        File diretorio = new File(caminhoDiretorio);

        if (!diretorio.exists()) {
            diretorio.mkdirs();
        } else {
            FileUtils.cleanDirectory(diretorio);
        }
        System.out.println("Diretorio: " + diretorio.getPath());
        File file = new File(diretorio.getPath(), nomeArquivo);

        InputStream InputStreamArquivoFile = arquivoMultipartFile.getInputStream();
        File arquivoFileTemp = new File("/var/lib/mysql-files/cargasvx/baixados/"+arquivoMultipartFile.getOriginalFilename());

        OutputStream outputStream = new FileOutputStream(arquivoFileTemp);
        IOUtils.copy(InputStreamArquivoFile, outputStream);
        outputStream.close();

        this.copiarArquivo(arquivoFileTemp,file);

    } catch (Exception e) {
       e.printStackTrace();
    }
}

When pass in:

        InputStream InputStreamArquivoFile = arquivoMultipartFile.getInputStream();

The exeption is throwable.

This is the stack:

java.lang.IllegalStateException: File has been moved - cannot be read again
at org.springframework.web.multipart.commons.CommonsMultipartFile.getInputStream(CommonsMultipartFile.java:146)
at br.com.sgsistemas.integrador.services.integracao.ImportacaoService.limparDiretorioCargasVXESalvarCarga(ImportacaoService.java:284)
at br.com.sgsistemas.integrador.services.integracao.ImportacaoService.iniciarImportacaoArquivos(ImportacaoService.java:155)
at br.com.sgsistemas.integrador.services.integracao.ImportacaoService$$FastClassBySpringCGLIB$$422ad70a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
at br.com.sgsistemas.integrador.services.integracao.ImportacaoService$$EnhancerBySpringCGLIB$$540126c6.iniciarImportacaoArquivos(<generated>)
at br.com.sgsistemas.integrador.controllers.ImportacaoAPI.lambda$iniciarImportacaoCarga$0(ImportacaoAPI.java:52)
at java.lang.Thread.run(Thread.java:748)

I know that i can setMaxInMemorySize() , but that is not the solution that i search. I need to save the File in some temp directory and after I manipulate it, delete it.

I'm not sure exactly what's going wrong in your code, but MultipartFile has a transferTo method that you can use to replace much of your code. By reducing the amount of code that you write, you will minimize the chance of a bug, and may perhaps see your issue go away...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM