.tmp文件未在Multipart Spring MVC文件上传中删除 [英] .tmp files not being deleted in Multipart Spring MVC File Upload

查看:102
本文介绍了.tmp文件未在Multipart Spring MVC文件上传中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了Spring MVC REST服务,该服务接受包含文件上传和JSON正文作为组成部分的多部分消息.以下是涉及的主要类:

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:

我的控制器:

    @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;
    }
}

我的Servlet初始化程序:

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;
    }
}

我的配置:

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

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

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

  }

我的问题是,在Servlet初始化程序(C:/temp/)中定义的临时位置包含.tmp文件夹,该文件夹是在对该服务的每次请求之后创建的,并且从未删除.在记事本中打开它们,看起来它们只包含请求中发送的JSON的纯文本副本,而不包含上载文件的字节.我无法终生解决如何使这些文件在处理后消失.目前,我只使用了 每次调用后都使用FileUtils.cleanDirectory("C/:temp/"),但对此解决方案我一点也不满意.有谁知道如何删除这些.tmp文件?

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?

推荐答案

我终于发现,无论出于何种原因,JVM都不会进行垃圾收集,因此.tmp文件将永久存在.尽管我的解决方案令人非常不满意,但我还是可以通过强烈建议JVM在文件上传结束时进行垃圾回收来解决此问题,

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!

这篇关于.tmp文件未在Multipart Spring MVC文件上传中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆