如何在 Spring Boot 中为文件上传指定临时目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29923682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one specify a temp directory for file uploads in Spring Boot?
提问by fivedogit
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 Boot,需要让用户上传文件进行处理。现在,文件上传到 /home/username/git/myproject 这不是很好。
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)?
我如何让 Spring 将这些文件上传到一个临时目录中,该目录将通过应用程序重启(或其他方式)定期清除?
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 I'm just running my app by executing Application and it's using embedded Tomcat.
PS我只是通过执行应用程序来运行我的应用程序,它使用嵌入式Tomcat。
UPDATE:
更新:
Ok I've got it sorted out. I was converting the incoming MultipartFile to a normal File like so:
好的,我已经解决了。我正在将传入的 MultipartFile 转换为普通文件,如下所示:
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;
}
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.
现在您可能会问,“那么 application.properties 文件的 'multipart.location' 设置怎么样?” 回想起来,该设置仅控制临时多部分文件的去向。如果您使用脚本查看该目录,您会看到一个“upload_.tmp”文件短暂出现然后消失。'multipart.location' 与您可能创建的任何持久文件对象无关。
(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?)
(注意,您可以使用上面的 MultipartBean 片段而不是 application.properties,但我没有尝试过,您为什么要尝试?)
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.
要更改真实临时目录的值,您可以使用“-Djava.io.tmp=/path/to/dir”VM 参数在运行 Spring Boot 应用程序之前指定您想要的任何内容。
回答by Qy Zuo
in springboot 1.4.1.RELEASE
在 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= ..
will be ok.
会好的。
回答by Leandro Carracedo
Since you are using Spring Boot it's easier to use the MultipartProperties
in your application.properties
file.
由于您使用的是 Spring Boot,因此MultipartProperties
在您的application.properties
文件中使用更容易。
From documentationproperties example:
从文档属性示例:
# MULTIPART (MultipartProperties)
multipart.enabled=true
multipart.file-size-threshold=0 # Threshold after which files will be written to disk.
multipart.location= # Intermediate location of uploaded files.
multipart.max-file-size=1Mb # Max file size.
multipart.max-request-size=10Mb # Max request size.
Also you could read a detailed description from the MultipartProperties.
您也可以从MultipartProperties 中阅读详细说明。
In order to configure to your system tmpdir, you could set:
为了配置到您的系统 tmpdir,您可以设置:
multipart.location=${java.io.tmpdir}
回答by igor.zh
If somebody is still looking for programmatic configuration:
如果有人仍在寻找程序化配置:
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
final ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
final String location = System.getProperty("java.io.tmpdir");
final long maxFileSize = 128*1024;
final long maxRequestSize = 128*1024;
final MultipartConfigElement multipartConfig = new MultipartConfigElement(location, maxFileSize, maxRequestSize, 0);
registration.setMultipartConfig(multipartConfig);
return registration;
}
}
回答by devcd603
On windows versus linux the temp dir could have a trailing slash. Multipart held tmp files that caused new file names. creating my own tmp dir solved the issue.
在 windows 和 linux 上,临时目录可能有一个斜杠。Multipart 持有导致新文件名的 tmp 文件。创建我自己的 tmp 目录解决了这个问题。
String tempDir = System.getProperty("java.io.tmpdir");
if( !tempDir.endsWith("/") && !tempDir.endsWith( "\") ) {
tempDir = tempDir+"/";