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工具类的更多相关文章

  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. WebApi生成在线API文档--Swagger

    1.前言 1.1 SwaggerUI SwaggerUI 是一个简单的Restful API 测试和文档工具.简单.漂亮.易用(官方demo).通过读取JSON 配置显示API. 项目本身仅仅也只依赖 ...

  2. js简单四则运算

    作业来源 本次作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2166 我的项目GitHub远程仓库地址:https:/ ...

  3. 品阿里 Java 开发手册有感

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 一个优秀的工程师和一个普通的工程师的区别,不是满天飞的架构图, ...

  4. Java基础系列-Stream

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10748817.html 一.概述 Stream操作简称流操作,这里的流与IO流毫无关系, ...

  5. ASCII Art ヾ(≧∇≦*)ゝ

    Conmajia, 2012 Updated on Feb. 18, 2018 What is ASCII art? It's graphic symbols formed by ASCII char ...

  6. Redis 过期键删除策略

    Redis 中数据库键的过期时间都保存在过期字典中,当一个键过期了,Redis 存在三种不同的删除策略:定时删除.惰性删除和定期删除 定时删除 定义 在设置键的过期时间的同时创建一个计时器,让定时器在 ...

  7. K3日志定时备份

    K3日志超过5万条以后,每次用户登陆后,系统都会提示日志太多.但是日志又不能随意删除,所以需要做个数据库定时任务,定时把日志转移到备份表. declare @dt datetime;; SELECT ...

  8. 《.NET 进阶指南》读书笔记2------定义不可改变类型

    不可改变对象的定义 一个类型的对象在创建后,它的状态就不能再改变,知道它死亡,它的状态一直维持与创建时相同.这时候称该对象具有不可改变性.这样的类型为不可改变类型. 不可改变对象在创建的时候,必须完全 ...

  9. 仿微信未读RecyclerView平滑滚动定位效果

    效果图有红点的地方表示有未读消息,依次双击首页图标定位,然后定位到某个未读在手动下滑一点距离在次点击定位效果 用过 RecyclerView 的人都知道,自带有几个滚动到item下标的方法,但是不靠谱 ...

  10. Android 报错:error: too many padding sections on bottom border

    一.发生错误 [我以为我做了一张完美的.9图片,没想到.9图片还需要画左边和上边,尴尬···] 二.解决方法 .9图片造成错误 [具体内容] 最后修改.9图为