").html(data).evalScripts();
195 |
196 | return data;
197 | }
198 | })
199 |
--------------------------------------------------------------------------------
/picture-server/src/main/java/com/cl/picture/controller/OpController.java:
--------------------------------------------------------------------------------
1 | package com.cl.picture.controller;
2 |
3 | import java.io.File;
4 | import java.io.FileOutputStream;
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.net.URLEncoder;
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | import javax.servlet.http.HttpServletRequest;
12 |
13 | import org.apache.commons.io.FilenameUtils;
14 | import org.springframework.beans.factory.annotation.Autowired;
15 | import org.springframework.stereotype.Controller;
16 | import org.apache.commons.lang.StringUtils;
17 | import org.springframework.web.bind.annotation.RequestMapping;
18 | import org.springframework.web.bind.annotation.RequestParam;
19 | import org.springframework.web.multipart.MultipartFile;
20 |
21 | import com.cl.picture.utils.ConfigUtil;
22 | import com.cl.picture.utils.ImageUtil;
23 |
24 |
25 |
26 | /**
27 | *对其他系统提供的文件上传、删除相关的控制器
28 | */
29 |
30 | @Controller
31 | public class OpController {
32 |
33 | @Autowired
34 | private ConfigUtil configUtil;
35 |
36 | @RequestMapping("/upload")
37 | public String upload(HttpServletRequest request,String dt,String no,String url,@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
38 | //文件保存成功后返回对方网站的页面,解决跨域的问题,url
39 |
40 | String resultJson = "{result:'success',message:'文件上传成功!'}";
41 |
42 | //判断传入的dt,no是否格式正确
43 | String regEx = "^\\d{6}$";
44 | Pattern pat = Pattern.compile(regEx);
45 | Matcher mat = pat.matcher(dt);
46 | if(!mat.find())
47 | {
48 | resultJson = "{result:'fail',message:'不是正确的年份或月份!'}";
49 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
50 | }
51 |
52 | regEx = "^\\d{8,10}$";
53 | pat = Pattern.compile(regEx);
54 | mat = pat.matcher(no);
55 | if(!mat.find())
56 | {
57 | resultJson = "{result:'fail',message:'不是正确的商品编号!'}";
58 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
59 | }
60 |
61 |
62 | //得到文件名
63 | String filename=uploadFile.getOriginalFilename();
64 |
65 | regEx = "^\\d{8,10}_[0,1]{1}\\d{1}_[b,o]{1}.\\w+$";
66 | pat = Pattern.compile(regEx);
67 | mat = pat.matcher(filename);
68 | if(!mat.find())
69 | {
70 | resultJson = "{result:'fail',message:'不是正确的图片格式!'}";
71 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
72 | }
73 |
74 | String ext = FilenameUtils.getExtension(filename).toLowerCase();
75 | String baseName = FilenameUtils.getBaseName(filename).toLowerCase();
76 | if(!ext.equals("jpg")
77 | && !ext.equals("png"))
78 | {
79 | resultJson = "{result:'fail',message:'不是正确的图片格式!'}";
80 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
81 | }
82 |
83 | if(StringUtils.isBlank(dt) || StringUtils.isBlank(no) || StringUtils.isBlank(url))
84 | {
85 | resultJson = "{result:'fail',message:'没有传入图片路径!'}";
86 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
87 | }
88 |
89 | if(uploadFile.getSize()>10000000)
90 | {
91 | resultJson = "{result:'fail',message:'文件不能大于10M!'}";
92 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
93 | }
94 |
95 | String pathdt = configUtil.getPictureLocation() + dt;
96 | File file =new File(pathdt);
97 | if(!file.exists())
98 | {
99 | Boolean r = file.mkdir();
100 | if(r)
101 | {
102 | //记录日志
103 | } else {
104 | //记录日志
105 | }
106 | }
107 | String path = pathdt + File.separator + no;
108 | file =new File(path);
109 | if(!file.exists())
110 | {
111 | Boolean r = file.mkdir();
112 | if(r)
113 | {
114 | //记录日志
115 | } else {
116 | //记录日志
117 | }
118 | }
119 |
120 | if(uploadFile.getSize()>0){
121 | try {
122 | SaveFileFromInputStream(uploadFile.getInputStream(),path,filename);
123 | } catch (IOException e) {
124 | resultJson = "{result:'fail',message:'"+e.getMessage()+"'}";
125 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
126 | }
127 | }
128 | else{
129 | resultJson = "{result:'fail',message:'上传文件不能为空'}";
130 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
131 | }
132 |
133 | //生成缩略图
134 | ImageUtil.CreateZoomPicture(path + File.separator + baseName + "." + ext,ext);
135 |
136 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
137 | }
138 |
139 | @RequestMapping("/delete")
140 | public String delete(HttpServletRequest request,String dt,String no,String url) throws Exception {
141 | //文件保存成功后返回对方网站的页面,解决跨域的问题,url
142 |
143 | String resultJson = "{}";
144 |
145 | //判断传入的dt,no是否格式正确
146 | String regEx = "^\\d{6}$";
147 | Pattern pat = Pattern.compile(regEx);
148 | Matcher mat = pat.matcher(dt);
149 | if(!mat.find())
150 | {
151 | resultJson = "{result:'fail',message:'不是正确的年份或月份!'}";
152 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
153 | }
154 |
155 | regEx = "^\\d{8,10}$";
156 | pat = Pattern.compile(regEx);
157 | mat = pat.matcher(no);
158 | if(!mat.find())
159 | {
160 | resultJson = "{result:'fail',message:'不是正确的商品编号!'}";
161 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
162 | }
163 |
164 | String pathdt = configUtil.getPictureLocation() + dt;
165 | String path = pathdt + File.separator + no;
166 | File file =new File(path);
167 | if(!file.exists())
168 | {
169 | resultJson = "{result:'fail',message:'指定的目录不存在!'}";
170 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
171 | }
172 |
173 | deleteDir(file);
174 |
175 | resultJson = "{result:'success',message:'指定的目录删除成功!'}";
176 | return "redirect:" + url + "?result=" + URLEncoder.encode(resultJson,"UTF-8");
177 | }
178 |
179 | /**保存文件
180 | * @param stream
181 | * @param path
182 | * @param filename
183 | * @throws IOException
184 | */
185 | private void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException
186 | {
187 | FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
188 | byte[] buffer =new byte[1024*1024];
189 | int byteread = 0;
190 | while ((byteread=stream.read(buffer))!=-1)
191 | {
192 | fs.write(buffer,0,byteread);
193 | fs.flush();
194 | }
195 | fs.close();
196 | stream.close();
197 | }
198 |
199 | /**
200 | * 递归删除目录下的所有文件及子目录下所有文件
201 | * @param dir 将要删除的文件目录
202 | * @return boolean Returns "true" if all deletions were successful.
203 | * If a deletion fails, the method stops attempting to
204 | * delete and returns "false".
205 | */
206 | private boolean deleteDir(File dir) {
207 | if (dir.isDirectory()) {
208 | String[] children = dir.list();
209 | for (int i=0; i
2 |
18 |
19 |
20 |
21 |
22 |
23 |
26 |
27 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
50 |
51 |
52 |
53 |
54 |
55 |
59 |
60 |
62 |
63 |
66 |
67 |
68 |
69 |
70 |
71 |
74 |
78 |
79 |
80 |
83 |
84 |
85 |
88 |
89 |
90 |
91 |
92 |
93 |
96 |
97 |
98 |
99 |
100 |
103 |
104 |
105 |
109 |
110 |
111 |
112 |
113 |
117 |
118 |
119 |
120 |
124 |
125 |
126 |
127 |
128 |
129 |
134 |
135 |
136 |
137 |
138 |
143 |
144 |
145 |
146 |
148 |
149 |
150 |
152 |
153 |
154 |
157 |
158 |
159 |
160 |
164 |
165 |
166 |
169 |
170 |
171 |
174 |
175 |
176 |
180 |
181 |
182 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
203 |
204 |
205 |
206 |
208 |
209 |
210 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
230 |
231 |
232 |
233 |
234 |
238 |
--------------------------------------------------------------------------------
/picture-server/src/main/java/com/cl/picture/utils/ReflectionUtil.java:
--------------------------------------------------------------------------------
1 | package com.cl.picture.utils;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 | import java.lang.reflect.Modifier;
7 | import java.lang.reflect.ParameterizedType;
8 | import java.lang.reflect.Type;
9 | import java.util.ArrayList;
10 | import java.util.Collection;
11 | import java.util.Date;
12 | import java.util.Iterator;
13 | import java.util.List;
14 |
15 | import org.apache.commons.beanutils.ConvertUtils;
16 | import org.apache.commons.beanutils.PropertyUtils;
17 | import org.apache.commons.beanutils.converters.DateConverter;
18 | import org.apache.commons.lang.StringUtils;
19 | import org.apache.log4j.Logger;
20 | import org.springframework.util.Assert;
21 |
22 | @SuppressWarnings("rawtypes")
23 | public class ReflectionUtil
24 | {
25 | private static Logger logger = Logger.getLogger(ReflectionUtil.class);
26 |
27 | public static Object invokeGetterMethod(Object target, String propertyName)
28 | {
29 | String getterMethodName = "get" + StringUtils.capitalize(propertyName);
30 | return invokeMethod(target, getterMethodName, new Class[0], new Object[0]);
31 | }
32 |
33 | public static void invokeSetterMethod(Object target, String propertyName, Object value)
34 | {
35 | invokeSetterMethod(target, propertyName, value, null);
36 | }
37 |
38 | public static void invokeSetterMethod(Object target, String propertyName, Object value, Class> propertyType)
39 | {
40 |
41 | Class type = (propertyType != null) ? propertyType : value.getClass();
42 | String setterMethodName = "set" + StringUtils.capitalize(propertyName);
43 | invokeMethod(target, setterMethodName, new Class[] { type }, new Object[] { value });
44 | }
45 |
46 | public static Object getFieldValue(Object object, String fieldName)
47 | {
48 | Field field = getDeclaredField(object, fieldName);
49 |
50 | if (field == null) {
51 | throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
52 | }
53 |
54 | makeAccessible(field);
55 |
56 | Object result = null;
57 | try {
58 | result = field.get(object);
59 | } catch (IllegalAccessException e) {
60 | logger.error("不可能抛出的异常{}", e);
61 | }
62 | return result;
63 | }
64 |
65 | public static void setFieldValue(Object object, String fieldName, Object value)
66 | {
67 | Field field = getDeclaredField(object, fieldName);
68 |
69 | if (field == null) {
70 | throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
71 | }
72 |
73 | makeAccessible(field);
74 | try
75 | {
76 | field.set(object, value);
77 | } catch (IllegalAccessException e) {
78 | logger.error("不可能抛出的异常:{}");
79 | }
80 | }
81 |
82 | public static void setFieldValueByFieldType(Object object, String fieldName, Object value)
83 | {
84 | Field field = getDeclaredField(object, fieldName);
85 |
86 | if (field == null) {
87 | throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
88 | }
89 | setFieldValue(object, fieldName, convertStringToObject((String)value, field.getType()));
90 | }
91 |
92 | public static Object invokeMethod(Object object, String methodName, Class>[] parameterTypes, Object[] parameters)
93 | {
94 | Method method = getDeclaredMethod(object, methodName, parameterTypes);
95 | if (method == null) {
96 | throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
97 | }
98 |
99 | method.setAccessible(true);
100 | try
101 | {
102 | return method.invoke(object, parameters);
103 | } catch (Exception e) {
104 | throw convertReflectionExceptionToUnchecked(e);
105 | }
106 | }
107 |
108 | public static Field getDeclaredField(Object object, String fieldName)
109 | {
110 | Assert.notNull(object, "object不能为空");
111 | Assert.hasText(fieldName, "fieldName");
112 | for (Class superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
113 | try
114 | {
115 | return superClass.getDeclaredField(fieldName);
116 | }
117 | catch (NoSuchFieldException e)
118 | {
119 | }
120 | return null;
121 | }
122 |
123 | protected static void makeAccessible(Field field)
124 | {
125 | if ((!(Modifier.isPublic(field.getModifiers()))) || (!(Modifier.isPublic(field.getDeclaringClass().getModifiers()))))
126 | field.setAccessible(true);
127 | }
128 |
129 | @SuppressWarnings("unchecked")
130 | protected static Method getDeclaredMethod(Object object, String methodName, Class>[] parameterTypes)
131 | {
132 | Assert.notNull(object, "object不能为空");
133 |
134 | for (Class superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
135 | try
136 | {
137 | return superClass.getDeclaredMethod(methodName, parameterTypes);
138 | }
139 | catch (NoSuchMethodException e)
140 | {
141 | }
142 | return null;
143 | }
144 |
145 | @SuppressWarnings("unchecked")
146 | public static Class getSuperClassGenricType(Class clazz)
147 | {
148 | return getSuperClassGenricType(clazz, 0);
149 | }
150 |
151 | public static Class getSuperClassGenricType(Class clazz, int index)
152 | {
153 | Type genType = clazz.getGenericSuperclass();
154 |
155 | if (!(genType instanceof ParameterizedType)) {
156 | logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
157 | return Object.class;
158 | }
159 |
160 | Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
161 |
162 | if ((index >= params.length) || (index < 0)) {
163 | logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length);
164 |
165 | return Object.class;
166 | }
167 | if (!(params[index] instanceof Class)) {
168 | logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
169 | return Object.class;
170 | }
171 |
172 | return ((Class)params[index]);
173 | }
174 |
175 | public static List convertElementPropertyToList(Collection collection, String propertyName)
176 | {
177 | List