huang
7 天以前 22e17b6db03ca58bc477a35ca067e55a09cffce7
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
package com.mes.utils;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.*;
 
 
/**
 * web工具类
 *
 * @author kong
 */
public class WebNbUtil {
 
    // 工具方法
 
    /**
     * 取出一个值,我保证不乱码,tomcat8及以上版本此方法废掉
     */
    public static String getParam(HttpServletRequest request, String key) {
        try {
            request.setCharacterEncoding("UTF-8");
            // 获得v
            String value = request.getParameter(key);
            if (value != null && "GET".equals(request.getMethod())) {
                value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
            }
            return value;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
 
    /**
     * 取出一个值,我保证不乱码,tomcat8及以上版本专用
     */
    public static String getParam8(HttpServletRequest request, String key) {
        try {
            if (request.getCharacterEncoding() == null) {
                request.setCharacterEncoding("UTF-8");
            }
            return request.getParameter(key);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
 
    /**
     * 将一个值,强制转码ISO_8859_1-->utf-8
     */
    public static String getIso88591(String str) {
        try {
            if (str == null) {
                return str;
            }
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
 
    /**
     * 将一个request请求所携带的参数封装成map返回
     * <br/>对于数组型参数,此方法不能正确获得值
     *
     * @param request
     * @return
     */
    public static Map<String, String> getParamsMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>(16);
        try {
            //获得K列表
            Enumeration<String> paramNames = request.getParameterNames();
            request.setCharacterEncoding("UTF-8");
            while (paramNames.hasMoreElements()) {
                try {
                    //获得k
                    String key = paramNames.nextElement();
                    //获得v
                    String value = request.getParameter(key);
                    if ("GET".equals(request.getMethod())) {
 
                    }
                    map.put(key, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return map;
    }
 
    /**
     * 将一个request请求所携带的参数封装成map返回 ,带集合的
     */
    public static Map<String, Object> getParamsMap2(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>(16);
        // 获取所有参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            try {
                // 获得values
                String[] values = parameterMap.get(key);
                if (values.length == 1) {
                    map.put(key, values[0]);
                } else {
                    List<String> list = new ArrayList<String>();
                    for (String v : values) {
                        list.add(v);
                    }
                    map.put(key, list);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }
 
 
    /**
     * 将一个request请求Header所携带的参数封装成map返回
     */
    public static Map<String, String> getHeaderMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>(16);
        try {
            //获得K列表
            Enumeration<String> paramNames = request.getHeaderNames();
            request.setCharacterEncoding("UTF-8");
            while (paramNames.hasMoreElements()) {
                try {
                    //获得k
                    String key = paramNames.nextElement();
                    //获得v
                    String value = request.getHeader(key);
                    if ("GET".equals(request.getMethod())) {
                        new String(value.getBytes("ISO-8859-1"), "UTF-8");
                    }
                    map.put(key, value);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return map;
    }
 
 
    /**
     * 返回请求端的IP地址
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        ip = checkIp(ip) ? ip : (
                checkIp(ip = request.getHeader("Proxy-Client-IP")) ? ip : (
                        checkIp(ip = request.getHeader("WL-Proxy-Client-IP")) ? ip :
                                request.getRemoteAddr()));
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
 
    private static boolean checkIp(String ip) {
        return NbUtil.isNull(ip) && !"unknown".equalsIgnoreCase(ip);
    }
 
    /**
     * 返回指定地址在服务器上的绝对路径
     */
    public static String getRealPath(HttpSession session, String path) {
        // 路径
        return session.getServletContext().getRealPath(path);
    }
 
    /**
     * 返回项目的http地址
     */
    public static String getHttpUrl(HttpServletRequest request, String port) {
 
        String path = request.getServletContext().getContextPath();
        StringBuffer url = request.getRequestURL();
        String http = url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
 
        if (port != null && !"".equals(port) && !"80".equals(port) && !"443".equals(port)) {
            if (http.endsWith(":" + port) == false) {
                http += ":" + port;
            }
        }
        http += path;
        if (http.endsWith("/") == false) {
            http += "/";
        }
 
        return http;
    }
 
 
    /**
     * 检测request请求是否为ajax
     */
    public static boolean isAjax(HttpServletRequest request) {
        return !(request.getHeader("x-requested-with") == null);
    }
 
    /**
     * 将指定key的参数转化为int类型,转化不了的给默认值
     */
    public static int getInt(HttpServletRequest request, String key, int defaultValue) {
        try {
            String argStr = request.getParameter(key);
            return Integer.valueOf(argStr);
        } catch (Exception e) {
            return defaultValue;
        }
    }
 
 
}