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. }

WEB工具类的更多相关文章

  1. Spring web 工具类 WebApplicationContextUtils

    概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...

  2. Spring工具类

    文件资源访问 1.统一资源访问接口 Resource 2.实现类 FileSystemResource 通过文件系统路径访问 ClassPathResource 通过classpath路径访问 Ser ...

  3. velocity merge作为工具类从web上下文和jar加载模板的两种常见情形

    很多时候,处于各种便利性或折衷或者通用性亦或是限制的原因,会借助于模板生成结果,在此介绍两种使用velocity merge的情形,第一种是和spring mvc一样,将模板放在velocityCon ...

  4. 适用于app.config与web.config的ConfigUtil读写工具类

    之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...

  5. 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本

    快速创建SpringBoot2.x应用之工具类自动创建web应用简介:使用构建工具自动生成项目基本架构 1.工具自动创建:http://start.spring.io/ 2.访问地址:http://l ...

  6. web中CookieUtils的工具类

    该类中包含Web开发中对Cookie的常用操作,如需要Copy带走 package com.project.utils; import java.io.UnsupportedEncodingExcep ...

  7. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  8. 提供Web相关的个工具类

    package com.opslab.util.web; import com.opslab.util.ConvertUtil;import com.opslab.util.StringUtil; i ...

  9. SON Web Tokens 工具类 [ JwtUtil ]

    pom.xml <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt< ...

随机推荐

  1. android学习---下拉刷新组建

    Google官方的下拉刷新组建 activity代码实现: /** * The SwipeRefreshLayout should be used whenever the user * can re ...

  2. javaScript设计模式--观察者模式(observer)

    观察者模式(observer):又被称为 发布-订阅者模式或者消息机制,定义了一种依赖关系,解决了主体对象与观察者之间功能耦合. 一.这样的需求 在实现自己的需求,而添加一些功能代码,但是又不想新添加 ...

  3. 《前端之路》之 JavaScript原型及原型链详解

    05:JS 原型链 在 JavaScript 的世界中,万物皆对象! 但是这各种各样的对象其实具体来划分的话就 2 种. 一种是 函数对象,剩下的就是 普通对象.其中 Function 和 Objec ...

  4. 前端笔记之移动端&响应式(上)媒体查询&Bootstrap&动画库&zepto&velocity

    一.媒体(介)查询 1.1 基本语法 媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于检测的媒体特性有:width.height和color(等).使用媒体查询可以在不改变 ...

  5. js事件循环机制辨析

     对于新接触js语言的人来说,最令人困惑的大概就是事件循环机制了.最开始这也困惑了我好久,花了我几个月时间通过书本,打代码,查阅资料不停地渐进地理解他.接下来我想要和大家分享一下,虽然可能有些许错误的 ...

  6. python基础2--数据结构(列表List、元组Tuple、字典Dict)

    1.Print函数中文编码问题 print中的编码:# -*- coding: utf-8 -*- 注:此处的#代表的是配置信息 print中的换行符,与C语言相同,为"\n" 2 ...

  7. AOP面向切面编程C#实例

    原创: eleven 原文:https://mp.weixin.qq.com/s/8klfhCkagOxlF1R0qfZsgg [前言] AOP(Aspect-Oriented Programming ...

  8. [转]nodejs日期时间插件moment.js

    本文转自:https://blog.csdn.net/dreamer2020/article/details/52278478 问题来源js自带的日期Date可以满足一些基本的需求,例如格式化.时间戳 ...

  9. vue element-ui 分页组件封装

    <template> <el-pagination @size-change="handleSizeChange" @current-change="h ...

  10. WEB前端需要了解的XML相关基础知识

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...