如何在Spring Boot中为文件上传指定一个临时目录? [英] How does one specify a temp directory for file uploads in Spring Boot?

查看:3570
本文介绍了如何在Spring Boot中为文件上传指定一个临时目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot,需要让用户上传文件进行处理.现在,该文件上传到/home/username/git/myproject,这不是很好.

I'm using Spring Boot and need to let users upload files for processing. Right now, the file uploads to /home/username/git/myproject which is not great.

我如何让Spring将这些文件上传到一个临时目录中,该目录将由应用程序重新启动(或其他某种方式)定期清除?

How do I make Spring put those file uploads into a temporary directory that will be periodically purged by application restart (or some other means)?

这是我尝试过的方法...但是它不起作用.文件仍然保存到我的工作目录中.

Here's what I've tried... but it doesn't work. File still saves to my working directory.

public class Application implements CommandLineRunner {

    /*
     * This doesn't seem to work.
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("128KB");
        factory.setMaxRequestSize("128KB");
        factory.setLocation(System.getProperty("java.io.tmpdir"));
        return factory.createMultipartConfig();
    }

/* other stuff, main(), etc */

}

PS我只是通过执行应用程序来运行我的应用程序,并且它使用的是嵌入式Tomcat.

PS I'm just running my app by executing Application and it's using embedded Tomcat.

更新:

好,我已经整理好了.我将传入的MultipartFile转换为普通文件,如下所示:

Ok I've got it sorted out. I was converting the incoming MultipartFile to a normal File like so:

private File convertMultipartFileToFile(MultipartFile file) throws IOException
    {    
        File convFile = new File(file.getOriginalFilename());
        convFile.createNewFile(); 
        FileOutputStream fos = new FileOutputStream(convFile); 
        fos.write(file.getBytes());
        fos.close(); 
        return convFile;
    }

相反,我应该在这样的指定临时目录中创建一个新文件:

Instead, I should have been creating a new File in the designated temporary directory like this:

private File convertMultipartFileToFile(MultipartFile file) throws IOException
    {    
        File convFile = File.createTempFile("temp", ".xlsx"); // choose your own extension I guess? Filename accessible with convFile.getAbsolutePath()
        FileOutputStream fos = new FileOutputStream(convFile); 
        fos.write(file.getBytes());
        fos.close(); 
        return convFile;
    }

现在您可能会问,那么application.properties文件的'multipart.location'设置如何?"回顾起来很明显,该设置仅控制临时的多部分文件的位置.如果您使用脚本观看该目录,您将看到一个"upload_.tmp"文件短暂出现然后消失了. 'multipart.location'与您可能创建的任何持久File对象无关.

Now you may be asking, "Well what about the 'multipart.location' setting of the application.properties file?" That setting, obvious in retrospect, controls only where the ephemeral multipart file goes. If you watch that directory with a script, you'll see that a 'upload_.tmp' file appears briefly and then disappears. 'multipart.location' has nothing to do with any persistent File objects you might create.

(注意,您也许可以从上方使用MultipartBean片段而不是application.properties,但我没有尝试过,为什么要这么做?)

(Note, you may be able to use the MultipartBean snippet from above instead of application.properties, but I didn't try it and why would you want to?)

要更改真实临时目录的值,可以在运行Spring Boot应用程序之前使用"-Djava.io.tmp =/path/to/dir" VM参数指定所需的内容.

To change the value of your true temp directory, you can use "-Djava.io.tmp=/path/to/dir" VM argument to specify whatever you want before running your Spring Boot application.

推荐答案

在springboot 1.4.1中发布

in springboot 1.4.1.RELEASE

spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true
spring.http.multipart.location= ..

会没事的.

这篇关于如何在Spring Boot中为文件上传指定一个临时目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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