Apache Maven 3.5.4
JDK-10.0.1
春季引导 2.0.4.发布
赢 10
我在src/main/resources/images/文件夹中有图像。当我在智能 J 中运行应用程序时,它工作正常,但是当我创建应用程序的 jar 时。它找不到图像文件。我尝试了以下方法都没有奏效。知道我如何检索图像文件吗?谢谢。
dogImageFile = new File("src/main/resources/images/dog.png");
dogClick = new Image(dogImageFile.toURI().toString());
dogImageFile = new File("\images\dog.png");
dogClick = new Image(dogImageFile.toURI().toString());
//Tried via @Value("classpath:dog.png") and ResourceLoader
//Also tried
dogClick= new Image(getClass().getClassLoader().getResource("src/main/resources/images/dog.png").toString(), true);
这与java版本和springboot版本无关。您需要关心已编译的类文件和资源文件的路径。
InputStream stream = getClass().getClassLoader().getResourceAsStream("static/xxx.pdf");
File targetFile = new File("xxx.pdf");
FileUtils.copyInputStreamToFile(stream, targetFile);
嗨,您可以使用此代码读取 jar 中的文件!
这样做了。
dogClick = new Image("/images/dog.png");