在Springboot项目中需要做合成图片的项目,比如证书什么的。需要在代码中调用static包下面的图片等信息,上代码。
Resource fileRource = new ClassPathResource("static/img/background.jpg");
Resource fileRource1 = new ClassPathResource("static/img/qrcode.jpg");
File backgroundFile = fileRource.getFile();
File qrcodeFile = fileRource1.getFile();
String backgroundPath = backgroundFile.getPath();
String qrCodePath = qrcodeFile.getPath();
用此方式获取图片的绝对地址,测试也没有问题。
但是当打成jar包后,运行就报错了。
把取图片绝对地址的方式改为
GetResource.class.getClassLoader().getResource(“static/img/background.jpg”).getPath();
就可以了,或者用InputStream来接收。
InputStream backgroundInput = new ClassPathResource("/static/img/background.jpg").getInputStream();
InputStream qrcodeInput = new ClassPathResource("/static/img/qrcode.jpg").getInputStream();