1. 利用classloader的getResourceAsStream方法
/**
* 转换图片为base64
* @param picPath 图片相对路径
* @return
*/
public static String getPicBASE64(String picPath) {
String content = null;
try {
InputStream fileForInput =ImageUtils.class.getClassLoader().getResourceAsStream("static/images/"+picPath);
byte[] bytes = new byte[fileForInput.available()];
fileForInput.read(bytes);
content = new sun.misc.BASE64Encoder().encode(bytes); // 具体的编码方法
fileForInput.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
2. 利用getResource方法,与第一种一样,只是需要自己转为inputstream