java - 如何使用 Java 在 heroku/docker 中创建临时目录?

标签 java spring-boot docker heroku temp

我正在尝试创建一个临时文件并生成一个文件名,然后保存一个多部分文件。我正在使用 Spring Boot,以下代码在本地运行。但是在 heroku 或 docker 中,它正在抛出 FileNotFoundException ;那么如何在 docker/heroku 中创建一个临时目录并将文件保存在该临时目录中呢? 或者什么是最好的保存方式multipart文件到服务器中的临时文件夹? 任何人都可以帮助我吗?提前致谢。

File tempDirectory = new File(new File(System.getProperty("java.io.tmpdir")), "files");

   if (!tempDirectory.exists()) {
       tempDirectory.mkdir();
   }

String filePath = new File(tempDirectory.getAbsolutePath() + "/temp.pdf").getAbsolutePath();

最佳答案

 File tempDirectory = new File(new File(System.getProperty("java.io.tmpdir")), "files");
        if(tempDirectory.exists()){
            System.out.println("something");
        }else{
            tempDirectory.mkdirs();
        }

        File file = new File(tempDirectory.getAbsolutePath()+"/abcd.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        String file2= new File(tempDirectory.getAbsolutePath()+"/something.txt").getAbsolutePath();


        System.out.println(file2);
在我结束时完全正常。您可能遇到的唯一问题是
String filePath = new File(tempDirectory.getAbsolutePath() + "/temp.exe").getAbsolutePath();
这不会在您创建的临时目录中创建文件。如果要将其保存在提到的目录中,它只会返回绝对路径。这可能是您收到 not found 错误的原因。尝试使用实际保存
file.transferTo(wherefileneedstobesavedlocation);

关于java - 如何使用 Java 在 heroku/docker 中创建临时目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67326911/

相关文章:

docker - Alpine Linux 处理证书的方式与 Busybox 不同吗?

java - 为什么我在这个 btree 方法中遇到 NullPointerException?

spring-boot - 如何在 Spring Boot 2.0 中注册我的自定义环境后处理器?

mysql - 使用docker-compose无法在同一网络中连接mysql和spring boot

docker - ModuleNotFoundError : No module named 'maskrcnn_benchmark'

node.js - docker compose extra_hosts 不改变/etc/hosts文件

java - Flying Saucer (xhtmlrenderer) 请求图像 4 次

java - 如何检查所有需要的订阅者是否完成了工作?

即使调用cancel方法后,Java线程也不会停止?

java - Spring Boot 项目中的 application.properties 文件在哪里?