如果要读取的文件在jar中,就会报错:java.lang.IllegalArgumentException: URI is not hierarchical
原因:jar包中的文件不能通过这种方式读取,因为目录是不透明的
原代码
final ClassPathResource classPathResource = new ClassPathResource("logo.png");
final File file = classPathResource.getFile();
Image srcImg = ImageIO.read(file);
解决方法:
final ClassPathResource classPathResource = new ClassPathResource("img/logo.png");
InputStream stream = classPathResource.getStream();
Image srcImg = ImageIO.read(stream);