.tmp files not being deleted in Multipart Spring MVC File Upload

白昼怎懂夜的黑 提交于 2020-06-25 21:36:52

问题


I've implemented a Spring MVC REST service that accepts a multipart message with both a file upload and a JSON body as the constitutive parts. Here are the main classes involved:

My Controller:

    @RestController
    public class MyController {

    @Autowired
    private MyService myService;

    @RequestMapping(value = "/publish", method = RequestMethod.POST,
            consumes = "multipart/form-data", produces = "application/json")
    public PublishContentResponse publishContent(@RequestPart("json") PublishContentRequest request, @RequestPart("file") MultipartFile file) throws IOException {
        PublishContentResponse response = myService.publishContent(request, file);
        return response;
    }
}

My Servlet Initializer:

    public class MyServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MyConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/mypath/*"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(getMultipartConfigElement());
    }

    private MultipartConfigElement getMultipartConfigElement() {
        loadServletProperties();
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement("c:/temp/", 5242880, 20971520, 0);
        return multipartConfigElement;
    }
}

My Config:

@Configuration
@ComponentScan
@EnableWebMvc
public class MyConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public MultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }

  }

My issues is that the temporary location defined in the servlet initializer (C:/temp/) contains .tmp folders that are created after every request to this service and are never deleted. Opening them up in notepad it looks like they only contain a plain text copy of the JSON sent in the request and not the bytes for the file uploaded. I can not for the life of me work out how to get these files to disappear after being processed. For now I have resorted to just using FileUtils.cleanDirectory("C/:temp/") after each call but I am not at all satisfied with this solution. Does anyone have any idea what I can do to get these .tmp files to delete?


回答1:


I finally found out that for whatever reason the JVM was not garbage collecting this so the .tmp files were persisting forever. Although an exceedingly unsatisfying solution I was able to fix this by strongly suggesting the JVM did garbage collect at the end of the file upload with

System.gc();

It would be good to still find a reason why it wasn't garbage collecting in the first place but I am giving up with this for now!



来源:https://stackoverflow.com/questions/41267833/tmp-files-not-being-deleted-in-multipart-spring-mvc-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!