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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.mandi.common;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.StringUtils;
 
/**
 * httpservletrequest。getparameter,并转换为响应的类型
 * @author Administrator
 *
 */
public class RequestParam {
    public static int getInt(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        int i=0;
        try {
            i=Integer.valueOf(tstr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;
    }
    private static float getFloat(HttpServletRequest request, String name) {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        if(tstr==null)
            return 0;
        float i=0.0f;
        try {
            i=Float.valueOf(tstr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;
 
    }
    private static byte getByte(HttpServletRequest request, String name) {
        String tstr=request.getParameter(name);
        if(tstr==null)
            return 0;
        tstr=StringUtils.trimToEmpty(tstr);
        byte i=0;
        try {
            i=Byte.valueOf(tstr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;
    }
    public static long getLong(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr==null)
            return 0;
        tstr=StringUtils.trimToEmpty(tstr);
        long i=0;
        try {
            i=Long.valueOf(tstr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;
    }
    public static double getDouble(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr==null)
            return 0;
        tstr=StringUtils.trimToEmpty(tstr);
        double i=0;
        try {
            i=Double.valueOf(tstr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return i;
    }
    public static Date getDate(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        Date tt=null;
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            tt=sdf.parse(tstr);
        } catch (Exception e) {
 
            try{
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                tt=sdf.parse(tstr);
            }catch(Exception ee){
                try{
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    tt=sdf.parse(tstr);
                }catch(Exception eee){
                    try{
                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
                        tt=sdf.parse(tstr);
                    }catch(Exception eeee){
                        
                    }
                }
            }
        }
        return tt;
    }
    public static Timestamp Timestamp(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        Timestamp tt=null;
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            tt=new Timestamp(sdf.parse(tstr).getTime());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return tt;
    }
    public static Time getTime(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        Time tt=null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
            tt=new Time(sdf.parse(tstr).getTime());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return tt;
    }
    public static boolean getBool(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        try {
            if("true".equals(tstr)||"1".equals(tstr.trim()))
                return true;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;
    }
    public static String getString(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        if(StringUtils.isBlank(tstr)){
            return null;
        }
        return tstr;
    }
    public static String getSqlString(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr!=null)
            tstr=StringUtils.trimToEmpty(tstr);
        if(tstr!=null)
            tstr=BasicMethod.sqlformat(tstr);
        if(StringUtils.isBlank(tstr)){
            return null;
        }
        return tstr;
    }
    public static String getURLString(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        if(tstr==null||tstr.equals(""))
            return "";
        try {
            String tstr1=new String(tstr.getBytes("ISO-8859-1"), "UTF-8");
            return tstr1.trim();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tstr;
    }
    /*
     * <script language="JavaScript">
document.write(encodeURI('http://www.blogjava.net/chenlb/abc 中文'));
</script>
 
String url = "http://www.blogjava.net/chenlb/abc%20%E4%B8%AD%E6%96%87";
        try {
            System.out.println(URLDecoder.decode(url, "UTF-8"));
            System.out.println(URLDecoder.decode(url, "GBK"));//乱码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
     */
    public static String getDecodeStr(HttpServletRequest request,String name)
    {
        String tstr=request.getParameter(name);
        try {
            tstr= URLDecoder.decode(tstr,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tstr;
    }
    public static <T extends Enum<T>> T getenum(HttpServletRequest r,String name,Class<T> clazz)
    {
        String tstr=r.getParameter(name);
        T t=null;
        try {
            t=Enum.valueOf(clazz, tstr);
        } catch (Exception e) {
        }
        return t;
    }
    public static <T> T getobj(HttpServletRequest r,T obj)
    {
        @SuppressWarnings("rawtypes")
        Class cls=obj.getClass();
        try {
            Field[] fs=cls.getDeclaredFields();
            for (Field field : fs) {
                field.setAccessible(true);
                String fn=field.getName();
                if(field.getType()==String.class)
                {
                    String t=RequestParam.getString(r, fn);
                    if(t!=null)
                        field.set(obj, t);
                }else if(field.getType()==Timestamp.class)
                {
                    Timestamp tt=RequestParam.Timestamp(r, fn);
                    if(tt!=null)
                        field.set(obj, tt);
                }else if(field.getType()==Date.class)
                {
                    Date dt=RequestParam.getDate(r, fn);
                    if(dt!=null)
                        field.set(obj, dt);
                }else if(field.getType()==byte.class||field.getType()==Byte.class)
                {
                    byte i=getByte(r, fn);
                    if(i!=0)
                        field.setByte(obj, i);
                }else if(field.getType()==float.class||field.getType()==Float.class)
                {
                    float i=getFloat(r, fn);
                    if(i!=0)
                        field.setFloat(obj, i);
                }else if(field.getType()==int.class||field.getType()==Integer.class)
                {
                    int i=getInt(r, fn);
                    if(i!=0)
                        field.setInt(obj, i);
                }else if(field.getType()==double.class||field.getType()==Double.class)
                {
                    Double d=getDouble(r, fn);
                    if(Math.abs(d)>0.0001)
                        field.setDouble(obj, d);
                }else if(field.getType()==long.class||field.getType()==Long.class)
                {
                    Long l=getLong(r, fn);
                    if(l!=0)
                        field.setLong(obj, l);
                }else if(field.getType()==Boolean.class||field.getType()==boolean.class)
                {
                    Boolean b=RequestParam.getBool(r, fn);
                    field.setBoolean(obj, b);
                }
                //System.out.println(field.getType().toString()+(field.getType()==int.class));
            }
        } catch (IllegalAccessException e) {
        }
        return obj;
    }
 
}