java.io.FileNotFoundException: class path resource [static/images/manZu.png]
springboot的项目在idea正常运行,打成jar包,报 java.io.FileNotFoundException,因为在打成jar包时,resources的路径会改变
/**
* 本地图片转换成base64字符串
* @param imageName 图片名称
* @reture 图片Base64
*/
public static String ImageToBase64ByLocal(String imageName) throws IOException {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
//获取图片路径
Resource resource = new ClassPathResource("/static/"+imageName);
File file = resource.getFile();
in = new FileInputStream(file.getPath());
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
Resource resource = new ClassPathResource("/static/"+imageName);
读取不到
改为
//获取图片路径
ClassPathResource resource = new ClassPathResource("/static/"+imageName);
in = resource.getInputStream();