【问题标题】:Springboot multipart file upload, remove the local server copySpring Boot 分段文件上传,移除本地服务器副本
【发布时间】:2018-04-16 04:16:00
【问题描述】:

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

# 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-boot multipart


    【解决方案1】:

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

    今天我面临同样的问题。 经过一番研究,事实证明 Spring 会自动执行此清理工作。 在我的情况下,清理没有完成,因为我忘记关闭与接收到的文件相关的流。

    希望对你有帮助。

    【讨论】:

    【解决方案2】:

    我将 multipart 转换为 Java File 对象并在使用后手动删除了它

    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.delete方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-22
      • 2021-03-28
      • 2021-06-05
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      相关资源
      最近更新 更多