有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AWS S3 tomcat 8上的设备没有剩余空间

我正在使用下面的代码来编写S3 bucket,它现在有很多xml文件。我收到设备上没有剩余空间的错误消息。随后的上传失败。我看到在var/cache/tomcat8/temp文件夹中创建了很多文件。我正在通过我的代码删除文件。有人能帮我弄清楚这里出了什么问题吗?实际上,我们想在上传到S3后立即删除临时文件

Path path = Files.createTempFile(filename, suffix);
file = path.toFile();

Files.write(path, incomingXml.getBytes(StandardCharsets.UTF_8));

if(file!=null){
    file.deleteOnExit();
}
s3client.putObject(new PutObjectRequest(fullBucketPathByDate, filename, file));
LOGGER.info("Upload completed");

共 (1) 个答案

  1. # 1 楼答案

    File.deleteOnExitdocs(我的重点):

    Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

    这意味着只有在关闭Tomcat进程后,临时文件才会被删除。您需要做的是在完成将文件上传到S3后立即删除该文件。比如:

    Path path = Files.createTempFile(filename, suffix);
    file = path.toFile();
    
    Files.write(path, incomingXml.getBytes(StandardCharsets.UTF_8));
    
    if(file!=null){
        s3client.putObject(new PutObjectRequest(fullBucketPathByDate, filename, file));
        file.delete();
    }
    LOGGER.info("Upload completed");
    

    或者,考虑直接将文件内容流式传输到S3,而不使用临时文件