1 import java.io.UnsupportedEncodingException; 2 import java.net.URLDecoder; 3 import java.net.URLEncoder; 4 import java.nio.charset.Charset; 5 import java.util.HashMap; 6 import java.util.Map; 7 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.commons.lang.StringUtils; 13 import org.springframework.util.Assert; 14 15 import com.qbskj.project.common.Setting; 16 17 /** 18 * Utils - Web 19 * 20 */ 21 public final class WebUtils { 22 23 /** 24 * 不可实例化 25 */ 26 private WebUtils() { 27 } 28 29 /** 30 * 添加cookie 31 * 32 * @param request 33 * HttpServletRequest 34 * @param response 35 * HttpServletResponse 36 * @param name 37 * cookie名称 38 * @param value 39 * cookie值 40 * @param maxAge 41 * 有效期(单位: 秒) 42 * @param path 43 * 路径 44 * @param domain 45 * 域 46 * @param secure 47 * 是否启用加密 48 */ 49 public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, 50 Integer maxAge, String path, String domain, Boolean secure) { 51 Assert.notNull(request); 52 Assert.notNull(response); 53 Assert.hasText(name); 54 try { 55 name = URLEncoder.encode(name, "UTF-8"); 56 value = URLEncoder.encode(value, "UTF-8"); 57 Cookie cookie = new Cookie(name, value); 58 if (maxAge != null) { 59 cookie.setMaxAge(maxAge); 60 } 61 if (StringUtils.isNotEmpty(path)) { 62 cookie.setPath(path); 63 } 64 if (StringUtils.isNotEmpty(domain)) { 65 cookie.setDomain(domain); 66 } 67 if (secure != null) { 68 cookie.setSecure(secure); 69 } 70 response.addCookie(cookie); 71 } catch (UnsupportedEncodingException e) { 72 e.printStackTrace(); 73 } 74 } 75 76 /** 77 * 添加cookie 78 * 79 * @param request 80 * HttpServletRequest 81 * @param response 82 * HttpServletResponse 83 * @param name 84 * cookie名称 85 * @param value 86 * cookie值 87 * @param maxAge 88 * 有效期(单位: 秒) 89 */ 90 public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, 91 Integer maxAge) { 92 Setting setting = SettingUtils.get(); 93 addCookie(request, response, name, value, maxAge, setting.getCookiePath(), setting.getCookieDomain(), null); 94 } 95 96 /** 97 * 添加cookie 98 * 99 * @param request 100 * HttpServletRequest 101 * @param response 102 * HttpServletResponse 103 * @param name 104 * cookie名称 105 * @param value 106 * cookie值 107 */ 108 public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) { 109 Setting setting = SettingUtils.get(); 110 addCookie(request, response, name, value, null, setting.getCookiePath(), setting.getCookieDomain(), null); 111 } 112 113 /** 114 * 获取cookie 115 * 116 * @param request 117 * HttpServletRequest 118 * @param name 119 * cookie名称 120 * @return 若不存在则返回null 121 */ 122 public static String getCookie(HttpServletRequest request, String name) { 123 Assert.notNull(request); 124 Assert.hasText(name); 125 Cookie[] cookies = request.getCookies(); 126 if (cookies != null) { 127 try { 128 name = URLEncoder.encode(name, "UTF-8"); 129 for (Cookie cookie : cookies) { 130 if (name.equals(cookie.getName())) { 131 return URLDecoder.decode(cookie.getValue(), "UTF-8"); 132 } 133 } 134 } catch (UnsupportedEncodingException e) { 135 e.printStackTrace(); 136 } 137 } 138 return null; 139 } 140 141 /** 142 * 移除cookie 143 * 144 * @param request 145 * HttpServletRequest 146 * @param response 147 * HttpServletResponse 148 * @param name 149 * cookie名称 150 * @param path 151 * 路径 152 * @param domain 153 * 域 154 */ 155 public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path, 156 String domain) { 157 Assert.notNull(request); 158 Assert.notNull(response); 159 Assert.hasText(name); 160 try { 161 name = URLEncoder.encode(name, "UTF-8"); 162 Cookie cookie = new Cookie(name, null); 163 cookie.setMaxAge(0); 164 if (StringUtils.isNotEmpty(path)) { 165 cookie.setPath(path); 166 } 167 if (StringUtils.isNotEmpty(domain)) { 168 cookie.setDomain(domain); 169 } 170 response.addCookie(cookie); 171 } catch (UnsupportedEncodingException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 /** 177 * 移除cookie 178 * 179 * @param request 180 * HttpServletRequest 181 * @param response 182 * HttpServletResponse 183 * @param name 184 * cookie名称 185 */ 186 public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) { 187 Setting setting = SettingUtils.get(); 188 removeCookie(request, response, name, setting.getCookiePath(), setting.getCookieDomain()); 189 } 190 191 /** 192 * 获取参数 193 * 194 * @param queryString 195 * 查询字符串 196 * @param encoding 197 * 编码格式 198 * @param name 199 * 参数名称 200 * @return 参数 201 */ 202 public static String getParameter(String queryString, String encoding, String name) { 203 String[] parameterValues = getParameterMap(queryString, encoding).get(name); 204 return parameterValues != null && parameterValues.length > 0 ? parameterValues[0] : null; 205 } 206 207 /** 208 * 获取参数 209 * 210 * @param queryString 211 * 查询字符串 212 * @param encoding 213 * 编码格式 214 * @param name 215 * 参数名称 216 * @return 参数 217 */ 218 public static String[] getParameterValues(String queryString, String encoding, String name) { 219 return getParameterMap(queryString, encoding).get(name); 220 } 221 222 /** 223 * 获取参数 224 * 225 * @param queryString 226 * 查询字符串 227 * @param encoding 228 * 编码格式 229 * @return 参数 230 */ 231 public static Map<String, String[]> getParameterMap(String queryString, String encoding) { 232 Map<String, String[]> parameterMap = new HashMap<String, String[]>(); 233 Charset charset = Charset.forName(encoding); 234 if (StringUtils.isNotEmpty(queryString)) { 235 byte[] bytes = queryString.getBytes(charset); 236 if (bytes != null && bytes.length > 0) { 237 int ix = 0; 238 int ox = 0; 239 String key = null; 240 String value = null; 241 while (ix < bytes.length) { 242 byte c = bytes[ix++]; 243 switch ((char) c) { 244 case '&': 245 value = new String(bytes, 0, ox, charset); 246 if (key != null) { 247 putMapEntry(parameterMap, key, value); 248 key = null; 249 } 250 ox = 0; 251 break; 252 case '=': 253 if (key == null) { 254 key = new String(bytes, 0, ox, charset); 255 ox = 0; 256 } else { 257 bytes[ox++] = c; 258 } 259 break; 260 case '+': 261 bytes[ox++] = (byte) ' '; 262 break; 263 case '%': 264 bytes[ox++] = (byte) ((convertHexDigit(bytes[ix++]) << 4) + convertHexDigit(bytes[ix++])); 265 break; 266 default: 267 bytes[ox++] = c; 268 } 269 } 270 if (key != null) { 271 value = new String(bytes, 0, ox, charset); 272 putMapEntry(parameterMap, key, value); 273 } 274 } 275 } 276 return parameterMap; 277 } 278 279 private static void putMapEntry(Map<String, String[]> map, String name, String value) { 280 String[] newValues = null; 281 String[] oldValues = map.get(name); 282 if (oldValues == null) { 283 newValues = new String[] { value }; 284 } else { 285 newValues = new String[oldValues.length + 1]; 286 System.arraycopy(oldValues, 0, newValues, 0, oldValues.length); 287 newValues[oldValues.length] = value; 288 } 289 map.put(name, newValues); 290 } 291 292 private static byte convertHexDigit(byte b) { 293 if ((b >= '0') && (b <= '9')) { 294 return (byte) (b - '0'); 295 } 296 if ((b >= 'a') && (b <= 'f')) { 297 return (byte) (b - 'a' + 10); 298 } 299 if ((b >= 'A') && (b <= 'F')) { 300 return (byte) (b - 'A' + 10); 301 } 302 throw new IllegalArgumentException(); 303 } 304 305 }