WEB工具类
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert; import com.qbskj.project.common.Setting; /**
* Utils - Web
*
*/
public final class WebUtils { /**
* 不可实例化
*/
private WebUtils() {
} /**
* 添加cookie
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param name
* cookie名称
* @param value
* cookie值
* @param maxAge
* 有效期(单位: 秒)
* @param path
* 路径
* @param domain
* 域
* @param secure
* 是否启用加密
*/
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge, String path, String domain, Boolean secure) {
Assert.notNull(request);
Assert.notNull(response);
Assert.hasText(name);
try {
name = URLEncoder.encode(name, "UTF-8");
value = URLEncoder.encode(value, "UTF-8");
Cookie cookie = new Cookie(name, value);
if (maxAge != null) {
cookie.setMaxAge(maxAge);
}
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
if (secure != null) {
cookie.setSecure(secure);
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} /**
* 添加cookie
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param name
* cookie名称
* @param value
* cookie值
* @param maxAge
* 有效期(单位: 秒)
*/
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
Integer maxAge) {
Setting setting = SettingUtils.get();
addCookie(request, response, name, value, maxAge, setting.getCookiePath(), setting.getCookieDomain(), null);
} /**
* 添加cookie
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param name
* cookie名称
* @param value
* cookie值
*/
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
Setting setting = SettingUtils.get();
addCookie(request, response, name, value, null, setting.getCookiePath(), setting.getCookieDomain(), null);
} /**
* 获取cookie
*
* @param request
* HttpServletRequest
* @param name
* cookie名称
* @return 若不存在则返回null
*/
public static String getCookie(HttpServletRequest request, String name) {
Assert.notNull(request);
Assert.hasText(name);
Cookie[] cookies = request.getCookies();
if (cookies != null) {
try {
name = URLEncoder.encode(name, "UTF-8");
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
} /**
* 移除cookie
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param name
* cookie名称
* @param path
* 路径
* @param domain
* 域
*/
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name, String path,
String domain) {
Assert.notNull(request);
Assert.notNull(response);
Assert.hasText(name);
try {
name = URLEncoder.encode(name, "UTF-8");
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
if (StringUtils.isNotEmpty(path)) {
cookie.setPath(path);
}
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} /**
* 移除cookie
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param name
* cookie名称
*/
public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
Setting setting = SettingUtils.get();
removeCookie(request, response, name, setting.getCookiePath(), setting.getCookieDomain());
} /**
* 获取参数
*
* @param queryString
* 查询字符串
* @param encoding
* 编码格式
* @param name
* 参数名称
* @return 参数
*/
public static String getParameter(String queryString, String encoding, String name) {
String[] parameterValues = getParameterMap(queryString, encoding).get(name);
return parameterValues != null && parameterValues.length > 0 ? parameterValues[0] : null;
} /**
* 获取参数
*
* @param queryString
* 查询字符串
* @param encoding
* 编码格式
* @param name
* 参数名称
* @return 参数
*/
public static String[] getParameterValues(String queryString, String encoding, String name) {
return getParameterMap(queryString, encoding).get(name);
} /**
* 获取参数
*
* @param queryString
* 查询字符串
* @param encoding
* 编码格式
* @return 参数
*/
public static Map<String, String[]> getParameterMap(String queryString, String encoding) {
Map<String, String[]> parameterMap = new HashMap<String, String[]>();
Charset charset = Charset.forName(encoding);
if (StringUtils.isNotEmpty(queryString)) {
byte[] bytes = queryString.getBytes(charset);
if (bytes != null && bytes.length > 0) {
int ix = 0;
int ox = 0;
String key = null;
String value = null;
while (ix < bytes.length) {
byte c = bytes[ix++];
switch ((char) c) {
case '&':
value = new String(bytes, 0, ox, charset);
if (key != null) {
putMapEntry(parameterMap, key, value);
key = null;
}
ox = 0;
break;
case '=':
if (key == null) {
key = new String(bytes, 0, ox, charset);
ox = 0;
} else {
bytes[ox++] = c;
}
break;
case '+':
bytes[ox++] = (byte) ' ';
break;
case '%':
bytes[ox++] = (byte) ((convertHexDigit(bytes[ix++]) << 4) + convertHexDigit(bytes[ix++]));
break;
default:
bytes[ox++] = c;
}
}
if (key != null) {
value = new String(bytes, 0, ox, charset);
putMapEntry(parameterMap, key, value);
}
}
}
return parameterMap;
} private static void putMapEntry(Map<String, String[]> map, String name, String value) {
String[] newValues = null;
String[] oldValues = map.get(name);
if (oldValues == null) {
newValues = new String[] { value };
} else {
newValues = new String[oldValues.length + 1];
System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
newValues[oldValues.length] = value;
}
map.put(name, newValues);
} private static byte convertHexDigit(byte b) {
if ((b >= '0') && (b <= '9')) {
return (byte) (b - '0');
}
if ((b >= 'a') && (b <= 'f')) {
return (byte) (b - 'a' + 10);
}
if ((b >= 'A') && (b <= 'F')) {
return (byte) (b - 'A' + 10);
}
throw new IllegalArgumentException();
} }
WEB工具类的更多相关文章
- Spring web 工具类 WebApplicationContextUtils
概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...
- Spring工具类
文件资源访问 1.统一资源访问接口 Resource 2.实现类 FileSystemResource 通过文件系统路径访问 ClassPathResource 通过classpath路径访问 Ser ...
- velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
很多时候,处于各种便利性或折衷或者通用性亦或是限制的原因,会借助于模板生成结果,在此介绍两种使用velocity merge的情形,第一种是和spring mvc一样,将模板放在velocityCon ...
- 适用于app.config与web.config的ConfigUtil读写工具类
之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...
- 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本
快速创建SpringBoot2.x应用之工具类自动创建web应用简介:使用构建工具自动生成项目基本架构 1.工具自动创建:http://start.spring.io/ 2.访问地址:http://l ...
- web中CookieUtils的工具类
该类中包含Web开发中对Cookie的常用操作,如需要Copy带走 package com.project.utils; import java.io.UnsupportedEncodingExcep ...
- 适用于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通用)>,现在重新整理一 ...
- 提供Web相关的个工具类
package com.opslab.util.web; import com.opslab.util.ConvertUtil;import com.opslab.util.StringUtil; i ...
- SON Web Tokens 工具类 [ JwtUtil ]
pom.xml <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt< ...
随机推荐
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- 一个box-sizing: border-box和felx混合使用中遇到的问题
之前在项目中遇到一个布局上很趣的问题(也可能是笔者才疏学浅,哈哈).布局大概是这样的: 外层包裹器:采用flex布局,并指定内部子弹性盒子元素水平显示 侧边栏:flex盒子的子元素,可收起和展开.展开 ...
- 挖一挖@Bean这个东西
有Bean得治 任何一个正常程序的访问都会在内存中创建非常多的对象,对象与对象之间还会出现很多依赖关系(一个处理业务逻辑的类中几乎都会使用到别的类的实例),一般的做法都是使用new关键字来创建对象,对 ...
- 谈谈我理解的SA——Systems Architecture
什么是SA? SA即Systems Architecture,是系统体系结构. 系统体系结构是定义系统的结构.行为和系统视图的概念模型.架构师将其系统的形式化描述或表示出来,以支持结构和行为的推理的方 ...
- 程序猿想聊天 - 創問 4C 團隊教練心得(二)
在第二天裡,主要談的是關於 Courage (勇氣) . Co-Create (共創) 的部分因為我們不是真實的團隊,就沒有繼續下去了 一早開始先回顧了一下前一天的內容,接下來我們就開始玩小遊戲 這個 ...
- Spring Tool Suite4(sts)复制粘贴卡顿(ctrl+v, ctrl+c)、按住ctrl也很卡
最近在看<Spring in Action, Fifth Edition>,下载了Spring Tool Suite4,在使用的过程中发现了一些问题: 只要在复制粘贴(ctrl+c, ct ...
- Activiti(一) activiti数据库表说明
activiti介绍: activiti是一个业务流程管理(BPM)框架.它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可执行流程语言框架.开发人员可以通过插件直接绘画出 ...
- :only-child和:only-of-type选择器的比较
:only-child 当元素是唯一的子元素,被选择. HTML代码: <body> <div class="x"> <div>第一个DIV&l ...
- Spring MVC(三)控制器获取页面请求参数以及将控制器数据传递给页面和实现重定向的方式
首先做好环境配置 在mvc.xml里进行配置 1.开启组件扫描 2.开启基于mvc的标注 3.配置试图处理器 <?xml version="1.0" encoding=&qu ...
- MongoDB学习(使用分组、聚合和映射-归并)
使用分组.聚合和映射-归并 MongoDB的强大功能之一,是直接在服务器对文档的值进行复杂的操作,而不用先发文档发送到客户端在进行处理. 结果分组 对大型数据集进行查询操作时,通常会根据文档的字段值对 ...