在春季内容中自定义文件位置

时间:2018-11-11 17:00:45

标签: spring spring-boot

我正在使用spring内容,我有一个保存图片和视频的实体,所以我希望将所有视频和图片存储在此目录home / user / photo_video_myram

photo_video_myram是文件夹,我希望所有文件都存储在该文件夹中,但是我不确定如何使用spring内容

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

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

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

当我运行该应用程序时,即使我上传文件,但它们没有存储在预期的文件夹中,它也没有显示错误,我也尝试将目录dir 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

1 个答案:

答案 0 :(得分:0)

好像您在fileSystemResourceLoader方法中击中NPE:

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

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