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返回 *
对于数组型参数,此方法不能正确获得值 * * @param request * @return */ public static Map getParamsMap(HttpServletRequest request) { Map map = new HashMap<>(16); try { //获得K列表 Enumeration 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 getParamsMap2(HttpServletRequest request) { Map map = new HashMap<>(16); // 获取所有参数 Map 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 list = new ArrayList(); 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 getHeaderMap(HttpServletRequest request) { Map map = new HashMap<>(16); try { //获得K列表 Enumeration 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; } } }