提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.lang.reflect.Field; |
|
5 |
import java.net.URLDecoder; |
|
6 |
import java.util.Date; |
|
7 |
import java.sql.Time; |
|
8 |
import java.sql.Timestamp; |
|
9 |
import java.text.SimpleDateFormat; |
|
10 |
|
|
11 |
import javax.servlet.http.HttpServletRequest; |
|
12 |
|
|
13 |
import org.apache.commons.lang3.StringUtils; |
|
14 |
|
|
15 |
/** |
|
16 |
* httpservletrequest。getparameter,并转换为响应的类型 |
|
17 |
* @author Administrator |
|
18 |
* |
|
19 |
*/ |
|
20 |
public class RequestParam { |
|
21 |
public static int getInt(HttpServletRequest request,String name) |
|
22 |
{ |
|
23 |
String tstr=request.getParameter(name); |
|
24 |
if(tstr!=null) |
|
25 |
tstr=StringUtils.trimToEmpty(tstr); |
|
26 |
int i=0; |
|
27 |
try { |
|
28 |
i=Integer.valueOf(tstr); |
|
29 |
} catch (Exception e) { |
|
30 |
// TODO: handle exception |
|
31 |
} |
|
32 |
return i; |
|
33 |
} |
|
34 |
private static float getFloat(HttpServletRequest request, String name) { |
|
35 |
String tstr=request.getParameter(name); |
|
36 |
if(tstr!=null) |
|
37 |
tstr=StringUtils.trimToEmpty(tstr); |
|
38 |
if(tstr==null) |
|
39 |
return 0; |
|
40 |
float i=0.0f; |
|
41 |
try { |
|
42 |
i=Float.valueOf(tstr); |
|
43 |
} catch (Exception e) { |
|
44 |
// TODO: handle exception |
|
45 |
} |
|
46 |
return i; |
|
47 |
|
|
48 |
} |
|
49 |
private static byte getByte(HttpServletRequest request, String name) { |
|
50 |
String tstr=request.getParameter(name); |
|
51 |
if(tstr==null) |
|
52 |
return 0; |
|
53 |
tstr=StringUtils.trimToEmpty(tstr); |
|
54 |
byte i=0; |
|
55 |
try { |
|
56 |
i=Byte.valueOf(tstr); |
|
57 |
} catch (Exception e) { |
|
58 |
// TODO: handle exception |
|
59 |
} |
|
60 |
return i; |
|
61 |
} |
|
62 |
public static long getLong(HttpServletRequest request,String name) |
|
63 |
{ |
|
64 |
String tstr=request.getParameter(name); |
|
65 |
if(tstr==null) |
|
66 |
return 0; |
|
67 |
tstr=StringUtils.trimToEmpty(tstr); |
|
68 |
long i=0; |
|
69 |
try { |
|
70 |
i=Long.valueOf(tstr); |
|
71 |
} catch (Exception e) { |
|
72 |
// TODO: handle exception |
|
73 |
} |
|
74 |
return i; |
|
75 |
} |
|
76 |
public static double getDouble(HttpServletRequest request,String name) |
|
77 |
{ |
|
78 |
String tstr=request.getParameter(name); |
|
79 |
if(tstr==null) |
|
80 |
return 0; |
|
81 |
tstr=StringUtils.trimToEmpty(tstr); |
|
82 |
double i=0; |
|
83 |
try { |
|
84 |
i=Double.valueOf(tstr); |
|
85 |
} catch (Exception e) { |
|
86 |
// TODO: handle exception |
|
87 |
} |
|
88 |
return i; |
|
89 |
} |
|
90 |
public static Date getDate(HttpServletRequest request,String name) |
|
91 |
{ |
|
92 |
String tstr=request.getParameter(name); |
|
93 |
Date tt=null; |
|
94 |
if(tstr!=null) |
|
95 |
tstr=StringUtils.trimToEmpty(tstr); |
|
96 |
try { |
|
97 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
98 |
tt=sdf.parse(tstr); |
|
99 |
} catch (Exception e) { |
|
100 |
|
|
101 |
try{ |
|
102 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
|
103 |
tt=sdf.parse(tstr); |
|
104 |
}catch(Exception ee){ |
|
105 |
try{ |
|
106 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); |
|
107 |
tt=sdf.parse(tstr); |
|
108 |
}catch(Exception eee){ |
|
109 |
try{ |
|
110 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); |
|
111 |
tt=sdf.parse(tstr); |
|
112 |
}catch(Exception eeee){ |
|
113 |
|
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
return tt; |
|
119 |
} |
|
120 |
public static Timestamp Timestamp(HttpServletRequest request,String name) |
|
121 |
{ |
|
122 |
String tstr=request.getParameter(name); |
|
123 |
Timestamp tt=null; |
|
124 |
if(tstr!=null) |
|
125 |
tstr=StringUtils.trimToEmpty(tstr); |
|
126 |
try { |
|
127 |
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
128 |
tt=new Timestamp(sdf.parse(tstr).getTime()); |
|
129 |
} catch (Exception e) { |
|
130 |
// TODO: handle exception |
|
131 |
} |
|
132 |
return tt; |
|
133 |
} |
|
134 |
public static Time getTime(HttpServletRequest request,String name) |
|
135 |
{ |
|
136 |
String tstr=request.getParameter(name); |
|
137 |
if(tstr!=null) |
|
138 |
tstr=StringUtils.trimToEmpty(tstr); |
|
139 |
Time tt=null; |
|
140 |
try { |
|
141 |
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); |
|
142 |
tt=new Time(sdf.parse(tstr).getTime()); |
|
143 |
} catch (Exception e) { |
|
144 |
// TODO: handle exception |
|
145 |
} |
|
146 |
return tt; |
|
147 |
} |
|
148 |
public static boolean getBool(HttpServletRequest request,String name) |
|
149 |
{ |
|
150 |
String tstr=request.getParameter(name); |
|
151 |
if(tstr!=null) |
|
152 |
tstr=StringUtils.trimToEmpty(tstr); |
|
153 |
try { |
|
154 |
if("true".equals(tstr)||"1".equals(tstr.trim())) |
|
155 |
return true; |
|
156 |
} catch (Exception e) { |
|
157 |
// TODO: handle exception |
|
158 |
} |
|
159 |
return false; |
|
160 |
} |
|
161 |
public static String getString(HttpServletRequest request,String name) |
|
162 |
{ |
|
163 |
String tstr=request.getParameter(name); |
|
164 |
if(tstr!=null) |
|
165 |
tstr=StringUtils.trimToEmpty(tstr); |
|
166 |
if(StringUtils.isBlank(tstr)){ |
|
167 |
return null; |
|
168 |
} |
|
169 |
return tstr; |
|
170 |
} |
|
171 |
public static String getSqlString(HttpServletRequest request,String name) |
|
172 |
{ |
|
173 |
String tstr=request.getParameter(name); |
|
174 |
if(tstr!=null) |
|
175 |
tstr=StringUtils.trimToEmpty(tstr); |
|
176 |
if(tstr!=null) |
|
177 |
tstr=BasicMethod.sqlformat(tstr); |
|
178 |
if(StringUtils.isBlank(tstr)){ |
|
179 |
return null; |
|
180 |
} |
|
181 |
return tstr; |
|
182 |
} |
|
183 |
public static String getURLString(HttpServletRequest request,String name) |
|
184 |
{ |
|
185 |
String tstr=request.getParameter(name); |
|
186 |
if(tstr==null||tstr.equals("")) |
|
187 |
return ""; |
|
188 |
try { |
|
189 |
String tstr1=new String(tstr.getBytes("ISO-8859-1"), "UTF-8"); |
|
190 |
return tstr1.trim(); |
|
191 |
} catch (UnsupportedEncodingException e) { |
|
192 |
// TODO Auto-generated catch block |
|
193 |
e.printStackTrace(); |
|
194 |
} |
|
195 |
return tstr; |
|
196 |
} |
|
197 |
/* |
|
198 |
* <script language="JavaScript"> |
|
199 |
document.write(encodeURI('http://www.blogjava.net/chenlb/abc 中文')); |
|
200 |
</script> |
|
201 |
|
|
202 |
String url = "http://www.blogjava.net/chenlb/abc%20%E4%B8%AD%E6%96%87"; |
|
203 |
try { |
|
204 |
System.out.println(URLDecoder.decode(url, "UTF-8")); |
|
205 |
System.out.println(URLDecoder.decode(url, "GBK"));//乱码 |
|
206 |
} catch (UnsupportedEncodingException e) { |
|
207 |
e.printStackTrace(); |
|
208 |
} |
|
209 |
*/ |
|
210 |
public static String getDecodeStr(HttpServletRequest request,String name) |
|
211 |
{ |
|
212 |
String tstr=request.getParameter(name); |
|
213 |
try { |
|
214 |
tstr= URLDecoder.decode(tstr,"UTF-8"); |
|
215 |
} catch (UnsupportedEncodingException e) { |
|
216 |
// TODO Auto-generated catch block |
|
217 |
e.printStackTrace(); |
|
218 |
} |
|
219 |
return tstr; |
|
220 |
} |
|
221 |
public static <T extends Enum<T>> T getenum(HttpServletRequest r,String name,Class<T> clazz) |
|
222 |
{ |
|
223 |
String tstr=r.getParameter(name); |
|
224 |
T t=null; |
|
225 |
try { |
|
226 |
t=Enum.valueOf(clazz, tstr); |
|
227 |
} catch (Exception e) { |
|
228 |
} |
|
229 |
return t; |
|
230 |
} |
|
231 |
public static <T> T getobj(HttpServletRequest r,T obj) |
|
232 |
{ |
|
233 |
@SuppressWarnings("rawtypes") |
|
234 |
Class cls=obj.getClass(); |
|
235 |
try { |
|
236 |
Field[] fs=cls.getDeclaredFields(); |
|
237 |
for (Field field : fs) { |
|
238 |
field.setAccessible(true); |
|
239 |
String fn=field.getName(); |
|
240 |
if(field.getType()==String.class) |
|
241 |
{ |
|
242 |
String t=RequestParam.getString(r, fn); |
|
243 |
if(t!=null) |
|
244 |
field.set(obj, t); |
|
245 |
}else if(field.getType()==Timestamp.class) |
|
246 |
{ |
|
247 |
Timestamp tt=RequestParam.Timestamp(r, fn); |
|
248 |
if(tt!=null) |
|
249 |
field.set(obj, tt); |
|
250 |
}else if(field.getType()==Date.class) |
|
251 |
{ |
|
252 |
Date dt=RequestParam.getDate(r, fn); |
|
253 |
if(dt!=null) |
|
254 |
field.set(obj, dt); |
|
255 |
}else if(field.getType()==byte.class||field.getType()==Byte.class) |
|
256 |
{ |
|
257 |
byte i=getByte(r, fn); |
|
258 |
if(i!=0) |
|
259 |
field.setByte(obj, i); |
|
260 |
}else if(field.getType()==float.class||field.getType()==Float.class) |
|
261 |
{ |
|
262 |
float i=getFloat(r, fn); |
|
263 |
if(i!=0) |
|
264 |
field.setFloat(obj, i); |
|
265 |
}else if(field.getType()==int.class||field.getType()==Integer.class) |
|
266 |
{ |
|
267 |
int i=getInt(r, fn); |
|
268 |
if(i!=0) |
|
269 |
field.setInt(obj, i); |
|
270 |
}else if(field.getType()==double.class||field.getType()==Double.class) |
|
271 |
{ |
|
272 |
Double d=getDouble(r, fn); |
|
273 |
if(Math.abs(d)>0.0001) |
|
274 |
field.setDouble(obj, d); |
|
275 |
}else if(field.getType()==long.class||field.getType()==Long.class) |
|
276 |
{ |
|
277 |
Long l=getLong(r, fn); |
|
278 |
if(l!=0) |
|
279 |
field.setLong(obj, l); |
|
280 |
}else if(field.getType()==Boolean.class||field.getType()==boolean.class) |
|
281 |
{ |
|
282 |
Boolean b=RequestParam.getBool(r, fn); |
|
283 |
field.setBoolean(obj, b); |
|
284 |
} |
|
285 |
//System.out.println(field.getType().toString()+(field.getType()==int.class)); |
|
286 |
} |
|
287 |
} catch (IllegalAccessException e) { |
|
288 |
} |
|
289 |
return obj; |
|
290 |
} |
|
291 |
|
|
292 |
} |