Springboot多部分文件上传,删除本地服务器副本



这些天我正在开发一个 rest API,它提供了上传图像的功能。一切正常,但是我的后端服务器位置被映像副本填满了。看起来春天会保留它上传的每张图像的本地副本。是否有任何选项可以禁用本地副本的保存。我快速浏览了文档,可以在下面找到多部分文件的属性。

# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of 
multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files 
are written to disk. Values can use the suffixes "MB" or "KB" to indicate 
megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded 
files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use 
the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values 
can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, 
respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the 
multipart request lazily at the time of file or parameter access.

有一个位置我们可以告诉系统复制本地副本,但没有禁用它的选项。你们对此有什么建议吗?我们是否需要一个单独的程序来清除这些本地映像副本并节省空间?

谢谢凯斯

我知道

这个话题有点旧,但它是我在谷歌搜索期间出现的第一个话题。

今天我面临同样的问题。经过一番研究,事实证明Spring会自动进行这种清洁。就我而言,没有完成清理,因为我忘记关闭与收到的文件相关的流。

我希望它有所帮助。

我将多部分转换为 Java 文件对象并在使用后手动删除了它

public static File convertMultiPartToFile(MultipartFile file) throws IOException {
    File convFile = new File(file.getOriginalFilename());
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
  }

然后调用文件删除方法

File tempFile = FileUtils.convertMultiPartToFile(file);
logger.info("Deleting temp file on path " + tempFile.getAbsolutePath());
boolean deleted = tempFile.delete();

最新更新