3

I am using Spring boot application. I have this structure in my resource folder

resources
 |__customers
      |__retail

I have to pass the path to this folder to one of the beans for writing files. The names of the files are dynamic so I cannot pass a predefined value to the file.

To do this I tried

@value(${classpath:resources/customer/retail} )
Resources resource;

// Also tried 
ResourceLoader loader = new FileSystemResourceLoader();

ResourceUtils.getURL(filePathDump).getPath().getClass().getResource(
    "indexingData/publication/analysis/dump"
);
// and several other options 

but it shows throw file not found or resource not found Exception, Now I checked the way path are defined for each one of them and I am positive they were all specified correctly

I need a way to pass this path to a folder in resources. Please help

Lucie
  • 127
  • 1
  • 2
  • 12

1 Answers1

1

you can autowire org.springframework.core.io.ResourceLoader and then get file as follows:

resourceLoader.getResource("classpath:customer/retail/someFile.txt")
Adrian
  • 2,984
  • 15
  • 27
  • As I have mentioned in the I have a dynamic file name So I need the path to the folder to append my file name on the fly. – Lucie Mar 29 '19 at 09:17
  • If you want the path then use `Path path = Paths.get(getClass().getClassLoader().getResource(fileName).toURI());` – sam Mar 29 '19 at 09:25
  • @Lucie you can do it on the fly. I suppose you have a service method where you are working with the files, so there you can invoke this `resourceLoader.getResource("classpath:folderNameInResource/" + fileName)` ... or I must have got you question wrong – Adrian Mar 29 '19 at 09:36
  • @Samim It returns null – Lucie Mar 29 '19 at 09:37
  • @Adrian I am using Paths.write() to write I file, Would I wont be able to use this path to write a file – Lucie Mar 29 '19 at 09:39