项目中注册需要一个默认头像.查了下如何取项目本地的资源
使用
ClassPathResource
类来取resources
目录下的静态资源.
try {
//resources下的image目录
ClassPathResource timeOutImage = new ClassPathResource("image/defaultAvatar.jpeg");
//org.apache.commons.io包下的工具类
byte[] fileContent = FileUtils.readFileToByteArray(timeOutImage.getFile());
//转成Base64编码
String avatarBase64String = Base64.encodeBase64String(fileContent);
//为了能在浏览器中直接显示 增加前缀 "data:image/jpeg;base64,"
avatarBase64String = "data:image/jpeg;base64,"+avatarBase64String;
//直接保存,mysql中为text类型
designerToDB.setDesignerImage(avatarBase64String);
}catch (Exception e){
e.printStackTrace();
}