If the file to be read is in jar, an error will be reported: java.lang.illegalargumentexception: URI is not hierarchical
Reason: the files in the jar package cannot be read in this way because the directory is opaque
original code
final ClassPathResource classPathResource = new ClassPathResource("logo.png");
final File file = classPathResource.getFile();
Image srcImg = ImageIO.read(file);
Solution:
final ClassPathResource classPathResource = new ClassPathResource("img/logo.png");
InputStream stream = classPathResource.getStream();
Image srcImg = ImageIO.read(stream);
Read More:
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- How to Solve Java Runtime (class file version 53.0)ERROR
- [Solved] Resource compilation failed (Failed to compile values resource file…
- How to Solve Springboot configurate environment file Error
- How to Download File via Response (Example Code)
- How to Solve EasyExcel3.0.5 Version Error
- How to Solve Error: Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
- How to Solve Mybatis error: invalid bound statement (not found)
- How to Solve classnotfoundexception error in spark without Hadoop runtime
- How to Solve Error:java.io.InvalidClassException
- How to Solve Swagger error: IllegalStateException
- How to Solve Flick operate Error: not serialized
- How to Solve Nosuchmethoderror Error
- How to Solve ERROR: Java 1.7 or later is required to run Apache Drill
- How to Solve Log4j 2.5 upgrade to 2.15 error
- How to Solve Mockito mockedStatic NotAMockException Error
- Mac: How to Solve ERROR launching ‘JD-GUI‘
- How to Solve Junit Unit Test Error: “No runnable methods“
- How to Solve Error: Invalid bound statement (not found)
- How to Solve zipkin Install Error in Windows