Read the resources resource and convert it to file
Project scenario:
read JSON file from resources directory to get file
Problem Description:
there are no problems in the local project. As a result, an error is reported after the spring boot hits jar
java.io.FileNotFoundException: class path resource [/static/xxxxx.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/ BOOT-INF/classes!/ static/xxxxxxx
Code example:
Resource resource = new ClassPathResource(PushFcmApnsProperties.androidFilePath);
FileInputStream serviceAccount = new FileInputStream(resource.getFile());
Cause analysis:
in the development and debugging stage, when there is no package, you can locate the resource file through the path. At this time, the resource file really exists in the local disk of the computer </ font>
After packaging, spring attempts to access the file system path, but cannot access the path in the jar. So report an error. At this time, the resource file is in the classes file in the jar package
for example:
Solution:
//Change the read method first via
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("relative path");
// Get the inputStream stream of the file and convert it to file to create a temporary file
File bathFile = File.createTempFile("filename", "file format");
FileUtils.copyInputStreamToFile(inputStream, bathFile);
Read More:
- How to Download File via Response (Example Code)
- How to Use filechannel to copy files
- [Javac compilation exception] javac compilation prompts that the package in jdk cannot be found error: package jdk.internal.org.objectweb.asm does not exist and error: cannot find symbol
- springboot jsp: There was an unexpected error (type=Not Found, status=404). No message available
- Springboot Project: How to Introduces Local Jar Package
- [Solved] Spring upload file Error: Multipartfile Transferto() reported an error FileNotFoundException
- [Solved] jar Run Error: no main manifest attribute
- [Solved] itextpdf Read PDF File Error: Rebuild failed: trailer not found.
- [Solved] POI Read excel Error: Your InputStream was neither an OLE2 stream, nor an OOXML stream
- Java implementation of inputsteam to Base64
- [Solved] Springboot Error: swagger-UI/index.html Access Error 404
- [Maven] maven filtering OTS parsing error incorrect file size in WOFF head [Two Methods to Solve]
- How to Solve Mybatis error: invalid bound statement (not found)
- [Solved] Error: A JNI error has occurred, please check your installation and try again Exception in thread
- [How to Solve] Invalid bound statement (not found)
- Resources is configured in the build of Maven project to prevent the failure of resource export
- [Solved] Failed to bind properties under ‘spring.servlet.multipart.file-size-threshold‘ to
- Cannot resolve reference to bean ‘sqlSessionFactory‘ while setting bean property ‘sqlSessionFactory‘
- Extracting JDBC tool class: JDBC utils
- [Solved] Mybatis Error: Could not find resource mybatis-conf.xml