1

我正在处理 spring 内容我有一个包含图片和视频的实体,所以我希望所有视频和图片都存储在这个目录 home/user/photo_video_myram

photo_video_myram 是文件夹,我希望所有文件都存储在那里,但我不确定如何处理 spring 内容

根据文档,我可以像这样创建 bean

@Bean
    File filesystemRoot() {
        try {
            return Files.createTempDirectory("photo_video_myram").toFile();
        } catch (IOException io) {}
        return null;
    }

    @Bean
    FileSystemResourceLoader fileSystemResourceLoader() {
        return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }

当我运行应用程序时,即使我上传文件但它们没有存储在预期的文件夹中,它也没有显示错误我也尝试完整目录 home/user/photo_video_myram 但是我无法创建 bean filesystemroot

我能解释一下存储文件的工作原理以及如何创建自己的自定义位置来存储所有图像和视频吗

我也试过这个

 @Bean
     File filesystemRoot() {

        try {
            return Files.createDirectory(Paths.get("/home/user/photo_video_myram")).toFile();
        } catch (IOException io) {}
        return null;
    }

但我得到了

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contentEntityRestController' defined in URL [jar:file:/home/user/.m2/repository/com/github/paulcwarren/spring-content-rest/0.4.0/spring-content-rest-0.4.0.jar!/internal/org/springframework/content/rest/controllers/ContentEntityRestController.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contentStoreService': Unsatisfied dependency expressed through method 'setFactories' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileContentStore': Unsatisfied dependency expressed through field 'loader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystemResourceLoader' defined in class path resource [gettingstarted/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.content.fs.io.FileSystemResourceLoader]: Factory method 'fileSystemResourceLoader' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1267) ~[spring-beans-5.0.9.RELEASE.jar
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileContentStore': Unsatisfied dependency expressed through field 'loader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystemResourceLoader' defined in class path resource [gettingstarted/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.content.fs.io.FileSystemResourceLoader]: Factory method 'fileSystemResourceLoader' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
:5.0.9.RELEASE]
    at or
4

1 回答 1

0

看起来您在fileSystemResourceLoader方法中遇到了 NPE:

Factory method 'fileSystemResourceLoader' threw exception; nested exception is java.lang.NullPointerException

我怀疑这是因为你的Files.createTempDirectory调用抛出了一个 IOException,你捕获并吃掉它,然后返回 null。

于 2020-09-23T06:35:03.377 回答