【问题标题】:customize file location in spring content在 spring 内容中自定义文件位置
【发布时间】:2018-11-11 17:00:44
【问题描述】:

我正在处理 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());
    }

当我运行应用程序时,即使我上传文件但它们没有存储在预期的文件夹中,它也没有显示错误

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

我也试过了

 @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

【问题讨论】:

  • 我认为您的直接问题是您在 IOException 处理程序上遇到了return null,因此 Spring 上下文初始化会爆炸。
  • 尝试在您的 application.properties 中设置以下属性:spring.servlet.multipart.location=/home/user/...
  • 好主意@Puneet 请解释这是如何工作的以及如何将每个文件的路径附加到我的实体照片中的文件“localpath”

标签: spring spring-boot spring-content-community-project


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2016-10-17
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多