**
使用Base64上传图片到本地数据库
创建接口
/**
* 上传头像
* @param id 用户id
* @param file 用户上传的图片
* @return
*/
String upload(Integer id, MultipartFile file) throws IOException;
创建接口实现
* 上传头像
* @param id
* @param file
* @return
*/
@Override
public String upload(Integer id, MultipartFile file) throws IOException {
//根据id查询
ZczyUser zczyUser = mapper.selectById(id);
//存储图片
String encode= "data:image/png;base64,"+Base64.encode(file.getBytes());
// System.out.println(encode);
zczyUser.setAvatar(encode);
mapper.updateById(zczyUser);
return encode;
}
书写控制器
@Autowired
private ZczyUserService zczyUserService;
@ApiOperation("上传头像")
@ApiParam(value = "上传头像",required = true)
@RequestMapping("upload/{id}")
public String upload(@PathVariable("id") Integer id ,@RequestParam (name = "file") MultipartFile file ) throws IOException {
return zczyUserService.upload(id,file);
}