/**
* 图片上传
*/
public Map<String, Object> uploading(MultipartFile file, String code) throws RuntimeException {
System.out.println("请求进来了");
Object obj = redisTemplate.opsForValue().get(code);
UserInfo userInfo = JSON.parseObject(JSON.toJSONString(obj), UserInfo.class);
String openid = userInfo.getOpenId();
Map<String, Object> map = new HashMap<>();
if (file == null || file.isEmpty()) {
map.put("result",false);
return map;
}
// openid="oLZ0d61_0pX8KsoyERE8X4uYeXSs";
// 查询数据库以检查用户是否存在
Student existingStudent = findByOpenid(openid);
if (existingStudent == null) {
map.put("result",false);
return map;
}
ByteArrayOutputStream out = null;
try {
InputStream inputStream = file.getInputStream();
out = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
map.put("result",false);
return map;
}
byte[] imgByte = out.toByteArray();
// 更新现有用户的img字段
existingStudent.setImg(imgByte);
boolean save = updateStudentInfo(existingStudent);
map.put("result", save);
return map;
}
spring boot图片上传,存到数据库
最新推荐文章于 2024-03-24 19:43:18 发布