Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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<FileItem> 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<String, Object> tf=null;
        try{
            list=sf.parseRequest(req);
            Iterator<FileItem> 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(width<rwidth||height<rheight)
                        {
                            jo.addProperty("code", 1);
                            jo.addProperty("errmsg", "上传的图片分辨率大于特定的宽度和高度::"+width+"*"+height);
                            resp.getWriter().write(g.toJson(jo));
                            return;
                        }
                    }else if("gt".equals(cop))
                    {
                        if(width>rwidth||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;
        }
    }    
}