package com.mandi.servlet; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.mandi.common.Jacksonmethod; import com.mandi.common.RequestParam; import com.mandi.servlet.file.FileLoad; import com.mandi.servlet.file.impl.FileLoadImpl; /** * @author mengly * @version 创建时间:2015年9月25日 下午5:09:08 * 类说明 */ public class Imageuploadservlet extends HttpServlet { private Logger log=Logger.getLogger(Imageuploadservlet.class); private static final long serialVersionUID = 1L; /** * cop:lt,gt,eq */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ApplicationContext a=WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()); if(a==null) return; DiskFileItemFactory dfif=new DiskFileItemFactory(); ServletFileUpload sf=new ServletFileUpload(dfif); sf.setHeaderEncoding("utf-8"); List list=null; String path1=req.getParameter("path"); int width=RequestParam.getInt(req, "width"); int height=RequestParam.getInt(req, "height"); String cop=RequestParam.getString(req, "cop"); int size=RequestParam.getInt(req, "size"); FileItem tfi=null; Map tf=null; try{ list=sf.parseRequest(req); Iterator it=list.iterator(); while(it.hasNext()) { FileItem fi=it.next(); if(fi.isFormField()) { String pname=fi.getFieldName(); String pvalue=fi.getString(); try{ if("path".equals(pname)) { path1=pvalue; } if("width".equals(pname)) { width=Integer.valueOf(pvalue); } if("height".equals(pname)) { height=Integer.valueOf(pvalue); } if("cop".equals(pname)) { cop=pvalue; } if("size".equals(pname)) { size=Integer.valueOf(pvalue); } }catch(Exception e1){log.info("转换错误!");} }else{ tfi=fi; } } }catch(Exception e){ log.info(e.getMessage()); } if(path1==null||path1.isEmpty()) path1="/temp"; String path=req.getServletContext().getRealPath(path1); File dir=new File(path); if(!dir.exists()||!dir.isDirectory()) dir.mkdirs(); FileLoad fl=a.getBean(FileLoadImpl.class); tf=fl.uploadFile_map(path1, req, tfi, true, size); resp.setCharacterEncoding("UTF-8"); resp.setContentType("application/json; charset=utf-8"); JsonObject jo=new JsonObject(); Gson g=new Gson(); if(tf!=null) { String furl=tf.get("url")+""; furl=req.getServletContext().getRealPath(furl); log.info("furl:"+furl); if(cop!=null&&!cop.isEmpty()&&width>0&&height>0) { //检查图片像素 try { //BufferedImage bi=Sanselan.getBufferedImage(new File(furl)); BufferedImage bi=ImageIO.read(new File(furl)); int rwidth=bi.getWidth(); int rheight=bi.getHeight(); if("lt".equals(cop)) { if(widthrwidth||height>rheight) { jo.addProperty("code", 1); jo.addProperty("errmsg", "上传的图片分辨率小于特定的宽度和高度::"+width+"*"+height); resp.getWriter().write(g.toJson(jo)); return; } }else if("eq".equals(cop)) { if(width!=rwidth||height!=rheight) { jo.addProperty("code", 1); jo.addProperty("errmsg", "上传的图片分辨率不等于特定的宽度和高度::"+width+"*"+height); resp.getWriter().write(g.toJson(jo)); return; } }else{ jo.addProperty("code", 1); jo.addProperty("errmsg", "系统无法判断您要如何判断图片分辨率!!"); resp.getWriter().write(g.toJson(jo)); return; } } catch (Exception e) { e.printStackTrace(); jo.addProperty("code", 1); jo.addProperty("errmsg", "上传的图片不能正常打开,请检查!!"); resp.getWriter().write(g.toJson(jo)); return; } } resp.getWriter().write(Jacksonmethod.tojson(tf, false)); }else{ jo.addProperty("code", 1); jo.addProperty("errmsg", "你上传的图片不符合规则不能保存!"); resp.getWriter().write(g.toJson(jo)); return; } } }