@AdaptBy(type=UploadAdaptor.class, args={"${app.root}/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", ""})
@POST
@Ok("json")
@At("/avatar")
public NutMap uploadAvatar(@Param("file")TempFile tf,
@Param("x1")String x,
@Param("y1")String y,
@Param("w")String w,
@Param("h")String h,
@Attr(scope=Scope.SESSION, value="sessionUser")Users id,
HttpSession session,
AdaptorErrorContext err,HttpServletResponse response,HttpServletRequest req) {
NutMap m =new NutMap();
System.out.print("userIduserIduserIduserIduserIduserIduserIduserIduserIduserIduserIduserIduserIduserIduserId"+id.getId());
Users profile = Daos.ext(dao, FieldFilter.locked(Users.class, "avatar")).fetch(Users.class, id.getId());
String picurl=id.getPic();
String a = jiequ(picurl);
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
String HAOMIAO= String.valueOf(System.currentTimeMillis()) ;
String newurl=id.getId()+HAOMIAO;
String hehe = dateFormat.format( now );
System.out.println("HAOMIAO"+HAOMIAO);
String msg = null;
if (err != null && err.getAdaptorErr() != null) {
msg = "文件大小不符合规定";
} else if (tf == null) {
msg = "空文件";
} else {
msg="成功";
try {
@SuppressWarnings("deprecation")
BufferedImage image = Images.read(tf.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Images.writeJpeg(image, out, 0.8f);
System.out.print("存入");
System.out.println("dada."+a);
ImageIO.write(image, "jpg", new File("e:/" +"temp/"+ a));
String ss="e:/temp/";
imgCut("e:/" +"temp/"+ a,11,11,80,80);
} catch(DaoException e) {
msg = "系统错误";
} catch (Throwable e) {
System.out.print("cuowu图片格式错误图片格式错误图片格式错误图片格式错误图片格式错误图片格式错误"+e);
msg = "图片格式错误";
}
}
if (msg != null)
Mvcs.getHttpSession().setAttribute("upload-error-msg", msg);
File f=new File("e:/temp/"+a);
File mm=new File("e:/temp/"+newurl+".png");
if(f.renameTo(mm))
{
System.out.println("修改成功!");
}
else
{
System.out.println("修改失败");
}
profile.setPic( "/static_img/"+newurl+".png");
Daos.ext(dao, FieldFilter.create(Users.class, null, "avatar", true)).update(profile);
m.setv("msg", msg);
m.setv("url", "/static_img/"+newurl+".png");
return m;
}
public String jiequ(String url){
int index = url.lastIndexOf("/");
//将字符串转为字符数组
char[] ch = url.toCharArray();
//根据 copyValueOf(char[] data, int offset, int count) 取得最后一个字符串
String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);
return lastString;
}
public void renameFile(String file, String toFile) {
File toBeRenamed = new File(file);
//检查要重命名的文件是否存在,是否是文件
if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
System.out.println("File does not exist: " + file);
return;
}
File newFile = new File(toFile);
//修改文件名
if (toBeRenamed.renameTo(newFile)) {
System.out.println("File has been renamed.");
} else {
System.out.println("Error renmaing file");
}
}
public static void imgCut(String srcImageFile, int x, int y, int desWidth,
int desHeight) {
try {
Image img;
ImageFilter cropFilter;
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getWidth();
int srcHeight = bi.getHeight();
if (srcWidth >= desWidth && srcHeight >= desHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
cropFilter = new CropImageFilter(x, y, desWidth, desHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(desWidth, desHeight,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
//输出文件
ImageIO.write(tag, "JPEG", new File(srcImageFile+"333.jpg"));
}
} catch (Exception e) {
e.printStackTrace();
}
}