由于我有点赶,就没有将按照标准去写service和serviceImpl了,请见谅
目录
1.准备阶段
1.1数据库
?1.2相关的类
? 1.2.1 UserImg与数据库的表相对应
? 1.2.2? RespBean用来返回json格式
? 1.2.3? UserImgMapper
?1.2.4?UserImgMapper.xml
2.Controller的相关方法
2.1 文件上传(前端给后端传来文件和该用户的id)
??2.2将数据库的图片以base64加密后返回给前端
??2.3查看历史头像,将数据库中该用户的所有头像以json格式返回给前端,图片仍然以base64进行了加密
目录
1.准备阶段
1.1数据库
?1.2相关的类
? 1.2.1 UserImg与数据库的表相对应
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserImg {
private String id;
private byte[] photo;
}
? 1.2.2? RespBean用来返回json格式
????????
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RespBean {
private long code;
private String msg;
private Object object;
public static RespBean success(String msg) {
return new RespBean(200, msg, null);
}
public static RespBean success(Object object) {
return new RespBean(200,null ,object);
}
public static RespBean success(String msg, Object object) {
return new RespBean(200, msg, object);
}
public static RespBean error(String msg) {
return new RespBean(500, msg, null);
}
public static RespBean error(String msg,Object object) {
return new RespBean(500, msg, object);
}
}
? 1.2.3? UserImgMapper
@Mapper
@Repository
public interface UserImgMapper extends BaseMapper<UserImg> {
void insertPhoto(@Param("e") UserImg photo);
List<UserImg> findAllImgById(String userId);
}
?1.2.4?UserImgMapper.xml
2.Controller的相关方法
2.1 文件上传(前端给后端传来文件和该用户的id)
@PostMapping("/user/addimg")
private RespBean addimg(@RequestParam("file") MultipartFile file, String userId){
try {
byte[] data;
data = file.getBytes();
UserImg photo = new UserImg();
photo.setId(userId);
photo.setPhoto(data);
userimgMapper.insertPhoto(photo);
}catch (Exception e){
e.printStackTrace();
return RespBean.error("上传失败");
}
return RespBean.success("上传成功");
}
?2.2将数据库的图片以base64加密后返回给前端
@RequestMapping("/user/change")
public RespBean change(String userId){
List<UserImg> allImgById = userimgMapper.findAllImgById(userId);
UserImg userImg = allImgById.get(allImgById.size()-1);
return RespBean.success(userImg);
}
?2.3查看历史头像,将数据库中该用户的所有头像以json格式返回给前端,图片仍然以base64进行了加密
@RequestMapping("/user/historyImg")
public RespBean historyImg(String userId){
List<UserImg> allImgById = userimgMapper.findAllImgById(userId);
return RespBean.success(allImgById);
}
?
|