valik
valik

Reputation: 2094

customize file location in spring content

I am working with spring content i have an entity that holds pictures and videos so i want to all videos and pictures to be stored in this directory home/user/photo_video_myram

photo_video_myram is folder i want all files to be stored there but im not sure how to go about it with spring content

according to the documentation i can create beans like this

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

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

when i run the app it shows no error even when i upload files but they are not stored in the expected folder i also tried to full dir home/user/photo_video_myram but with that i get cannot create bean filesystemroot

can i get an explanation on how storing files work and how i can create my own customized location where all images and videos will be stored

I also tried this

 @Bean
     File filesystemRoot() {

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

but i got

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

Upvotes: 1

Views: 578

Answers (1)

Paul Warren
Paul Warren

Reputation: 2479

Looks like you are hitting a NPE inside the fileSystemResourceLoader method:

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

I suspect this is because your Files.createTempDirectory call is throwing an IOException that you catch and eat, then returning null.

Upvotes: 0

Related Questions