@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);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Images.writeJpeg(image, out, 0.8f);
profile.setAvatar(out.toByteArray());
dao.update(profile, "^avatar$");
} catch(DaoException e) {
log.info("System Error", e);
msg = "系统错误";
} catch (Throwable e) {
msg = "图片格式错误";
}
}
if (msg != null)
Mvcs.getHttpSession().setAttribute("upload-error-msg", msg);
}
添加回复
请先登陆