上传头像的入口方法
优质
小牛编辑
133浏览
2023-12-01
在UserProfileModule中新增一个方法,带适配器的哦
@AdaptBy(type=UploadAdaptor.class, args={"${app.root}/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400"})
@POST
@Ok(">>:/user/profile")
@At("/avatar")
public void uploadAvatar(@Param("file")TempFile tf,
@Attr(scope=Scope.SESSION, value="me")int userId,
AdaptorErrorContext err) {
String msg = null;
if (err != null && err.getAdaptorErr() != null) {
msg = "文件大小不符合规定";
} else if (tf == null) {
msg = "空文件";
} else {
UserProfile profile = get(userId);
try {
BufferedImage image = Images.read(tf.getFile());
image = Images.zoomScale(image, 128, 128, Color.WHITE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Images.writeJpeg(image, out, 0.8f);
profile.setAvatar(out.toByteArray());
dao.update(profile, "^avatarquot;);
} catch(DaoException e) {
log.info("System Error", e);
msg = "系统错误";
} catch (Throwable e) {
msg = "图片格式错误";
}
}
if (msg != null)
Mvcs.getHttpSession().setAttribute("upload-error-msg", msg);
}