Spring Download - Read a text file using ResourceLoader

I am trying to read a text file using the Spring Resource Loader as follows:

Resource resource = resourceLoader.getResource("classpath:\\static\\Sample.txt"); 

The file is here in my Spring boot project: enter image description here

It works fine when running the application in eclipse, but when I package the application, run it using java -jar, I get a file exception not found:

 java.io.FileNotFoundException: class path resource [static/Sample.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/workspace-test/XXX/target/XXX-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/Sample.txt 

I unzipped the Jar file that contains the Sample: XXX-0.0.1-SNAPSHOT \ BOOT-INF \ classes \ static \ Sample.txt

Can anybody help me?

Thanks in advance!

+6
source share
3 answers

I checked your code. If you want to load a file from the classpath in the Spring Boot JAR, then you should use resource.getInputStream () , not resource.getFile () . If you try to use resource.getFile (), you will get an error message because Spring is trying to access the path to the file system, but cannot access the path in your JAR.

Details as below:

https://smarterco.de/java-load-file-classpath-spring-boot/

+12
source

Try resourceLoader.getResource("classpath:static/Sample.txt");

Work with this code when starting with java -jar XXXX.jar

enter image description here

------ update ------

After going through your codes, the real problem is that you are trying to read the file using FileInputStream , but actually it is inside the jar file.

But actually you get org.springframework.core.io.Resource , so you really use InputStream, so you can do it like new BufferedReader(new InputStreamReader(resource.getInputStream())).readLine();

0
source

I had the same problem as @Gipple Lake explained, when loading Spring you need to load the file as input stream. So, I will add my code as an example, where I want to read the import.xml file

 public void init() { Resource resource = new ClassPathResource("imports/imports.xml"); try { InputStream dbAsStream = resource.getInputStream(); try { document = readXml(dbAsStream); } catch (SAXException e) { trace.error(e.getMessage(), e); e.printStackTrace(); } catch (ParserConfigurationException e) { trace.error(e.getMessage(), e); e.printStackTrace(); } } catch (IOException e) { trace.error(e.getMessage(), e); e.printStackTrace(); } initListeImports(); initNewImports(); } public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setIgnoringComments(false); dbf.setIgnoringElementContentWhitespace(true); dbf.setNamespaceAware(true); DocumentBuilder db = null; db = dbf.newDocumentBuilder(); return db.parse(is); } 

I added " imports.xml " below src/main/ressources/imports

0
source

Source: https://habr.com/ru/post/1014243/


All Articles