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:
- try-with-resource automatically closes the resources of the AutoClosable interface
- Resources is configured in the build of Maven project to prevent the failure of resource export
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- [Solved] Spring upload file Error: Multipartfile Transferto() reported an error FileNotFoundException
- [Solved] SpringBoot Pack Project: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources
- How to convert a Java string into a number (stringtonumber)
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- JAVA: How to Convert PDF from Color to Grayscale
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- JAVA 8: How to Convert List to Map
- [Solved] Resource compilation failed (Failed to compile values resource file…
- How to Solve Error:java.io.InvalidClassException
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] Error:maven-resources-production:ruoyi-ywjs: java.lang.NegativeArraySizeException
- How to Solve The classpathresource reads the resource file Error
- BeanDefinitionStoreException: Failed to read candidate component class probably due to a new Java
- Java.sql.sqlexception: unable to read more data from socket
- [Solved] the resource is not on the build path of a java project
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`