hjg
2024-03-18 8d48c59d5d3bcc4148ef97dc6f98e3a8cf9ca436
提交 | 用户 | 时间
58d006 1 package com.mandi.servlet;
A 2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.Enumeration;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.imageio.ImageIO;
12 import javax.servlet.ServletException;
13 import javax.servlet.annotation.WebServlet;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17
18 import org.apache.commons.fileupload.FileItem;
19 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
20 import org.apache.commons.fileupload.servlet.ServletFileUpload;
21 import org.apache.log4j.Logger;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.web.context.support.WebApplicationContextUtils;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonObject;
27 import com.mandi.common.Jacksonmethod;
28 import com.mandi.common.RequestParam;
29 import com.mandi.servlet.file.FileLoad;
30 import com.mandi.servlet.file.impl.FileLoadImpl;
31
32 /** 
33  * @author mengly 
34  * @version 创建时间:2015年9月25日 下午5:09:08 
35  * 类说明 
36  */
37
38 public class Imageuploadservlet  extends HttpServlet {
39     private Logger log=Logger.getLogger(Imageuploadservlet.class);
40     private static final long serialVersionUID = 1L;
41    /**
42     * cop:lt,gt,eq
43     */
44     @Override
45     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
46             throws ServletException, IOException {
47         ApplicationContext a=WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
48         if(a==null)
49             return;
50         DiskFileItemFactory dfif=new DiskFileItemFactory(); 
51         ServletFileUpload  sf=new ServletFileUpload(dfif);
52         sf.setHeaderEncoding("utf-8");
53         List<FileItem> list=null;
54         
55         String path1=req.getParameter("path");
56         int width=RequestParam.getInt(req, "width");
57         int height=RequestParam.getInt(req, "height");
58         String cop=RequestParam.getString(req, "cop");
59         int size=RequestParam.getInt(req, "size");
60         FileItem tfi=null;
61         Map<String, Object> tf=null;
62         try{
63             list=sf.parseRequest(req);
64             Iterator<FileItem> it=list.iterator();
65             while(it.hasNext())
66             {
67                 FileItem fi=it.next();
68                 if(fi.isFormField())
69                 {
70                     String pname=fi.getFieldName();
71                     String pvalue=fi.getString();
72                     try{
73                         if("path".equals(pname))
74                         {
75                             path1=pvalue;
76                         }
77                         if("width".equals(pname))
78                         {
79                             width=Integer.valueOf(pvalue);
80                         }
81                         if("height".equals(pname))
82                         {
83                             height=Integer.valueOf(pvalue);
84                         }
85                         if("cop".equals(pname))
86                         {
87                             cop=pvalue;
88                         }
89                         if("size".equals(pname))
90                         {
91                             size=Integer.valueOf(pvalue);
92                         }
93                     }catch(Exception e1){log.info("转换错误!");}
94                 }else{
95                     tfi=fi;
96                 }
97             }
98         }catch(Exception e){
99             log.info(e.getMessage());
100         }
101         if(path1==null||path1.isEmpty())
102             path1="/temp";
103         String    path=req.getServletContext().getRealPath(path1);
104         File dir=new File(path);
105         if(!dir.exists()||!dir.isDirectory())
106             dir.mkdirs();
107         FileLoad fl=a.getBean(FileLoadImpl.class);
108         tf=fl.uploadFile_map(path1, req, tfi, true, size);
109         
110         
111         resp.setCharacterEncoding("UTF-8");
112         resp.setContentType("application/json; charset=utf-8");
113         JsonObject jo=new JsonObject();
114         Gson g=new Gson();
115         if(tf!=null)
116         {
117             String furl=tf.get("url")+"";
118             furl=req.getServletContext().getRealPath(furl);
119             log.info("furl:"+furl);
120             
121             if(cop!=null&&!cop.isEmpty()&&width>0&&height>0)
122             {
123                 //检查图片像素
124                 try {
125                     //BufferedImage bi=Sanselan.getBufferedImage(new File(furl));
126                     BufferedImage bi=ImageIO.read(new File(furl));
127                     int rwidth=bi.getWidth();
128                     int rheight=bi.getHeight();
129                     if("lt".equals(cop))
130                     {
131                         if(width<rwidth||height<rheight)
132                         {
133                             jo.addProperty("code", 1);
134                             jo.addProperty("errmsg", "上传的图片分辨率大于特定的宽度和高度::"+width+"*"+height);
135                             resp.getWriter().write(g.toJson(jo));
136                             return;
137                         }
138                     }else if("gt".equals(cop))
139                     {
140                         if(width>rwidth||height>rheight)
141                         {
142                             jo.addProperty("code", 1);
143                             jo.addProperty("errmsg", "上传的图片分辨率小于特定的宽度和高度::"+width+"*"+height);
144                             resp.getWriter().write(g.toJson(jo));
145                             return;
146                         }
147                     }else if("eq".equals(cop))
148                     {
149                         if(width!=rwidth||height!=rheight)
150                         {
151                             jo.addProperty("code", 1);
152                             jo.addProperty("errmsg", "上传的图片分辨率不等于特定的宽度和高度::"+width+"*"+height);
153                             resp.getWriter().write(g.toJson(jo));
154                             return;
155                         }
156                     }else{
157                         jo.addProperty("code", 1);
158                         jo.addProperty("errmsg", "系统无法判断您要如何判断图片分辨率!!");
159                         resp.getWriter().write(g.toJson(jo));
160                         return;
161                     }
162                     
163                 } catch (Exception e) {
164                     e.printStackTrace();
165                     jo.addProperty("code", 1);
166                     jo.addProperty("errmsg", "上传的图片不能正常打开,请检查!!");
167                     resp.getWriter().write(g.toJson(jo));
168                     return;
169                 }
170             }
171             resp.getWriter().write(Jacksonmethod.tojson(tf, false));
172         }else{
173             jo.addProperty("code", 1);
174             jo.addProperty("errmsg", "你上传的图片不符合规则不能保存!");
175             resp.getWriter().write(g.toJson(jo));
176             return;
177         }
178     }    
179 }