代码之家  ›  专栏  ›  技术社区  ›  valik

如何使用spring boot为我的文件创建永久目录

  •  1
  • valik  · 技术社区  · 7 年前

    我正在使用spring boot和spring内容。我想将所有图片和视频存储在一个目录中,但每次重新运行应用程序时,我的代码都会继续创建不同的目录

    every time i run this tries to create the dir again
        @Bean
         File filesystemRoot() {
            try {
                return Files.createDirectory(Paths.get("/tmp/photo_video_myram")).toFile();
            } catch (IOException io) {}
            return null;
        }
    
        @Bean
        FileSystemResourceLoader fileSystemResourceLoader() {
            return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
        }
    
    2 回复  |  直到 5 年前
        1
  •  2
  •   Ortomala Lokni    7 年前

    @Bean
    File filesystemRoot() {
      File tmpDir = new File("tmp/photo_video_myram");
      if (!tmpDir.isDirectory()) {
        try {
          return Files.createDirectory(tmpDir.toPath()).toFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return tmpDir;
    }
    
        2
  •  2
  •   Raman Preet Singh    7 年前

    您可以首先使用isDirectory()方法检查目录是否已经存在。如果它不存在,则创建一个新的。

        3
  •  2
  •   marc_s MisterSmith    5 年前

    根据 https://paulcwarren.github.io/spring-content/refs/release/fs-index.html#_spring_boot_configuration

    spring.content.fs.filesystemRoot=/tmp/photo_video_myram
    

    application.properties 文件