1

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
Paul Warren
  • 2,411
  • 1
  • 15
  • 22
valik
  • 2,014
  • 4
  • 24
  • 55
  • I think your immediate problem is that you're hitting the `return null` on your IOException handler, and so Spring context init blows up. – moilejter Nov 11 '18 at 19:14
  • try setting below property in your application.properties:spring.servlet.multipart.location=/home/user/... – Puneet Nov 12 '18 at 00:51
  • nice idea @Puneet please explain how this works and how i can attach path to each file to a file "localpath" in my entity photo – valik Nov 12 '18 at 19:29

1 Answers1

0

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.

Paul Warren
  • 2,411
  • 1
  • 15
  • 22