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. 从ASP.NET Core2.2到3.0你可能会遇到这些问题

    趁着假期的时间所以想重新学习下微软的官方文档来巩固下基础知识.我们都知道微软目前已经发布了.NET Core3.0的第三个预览版,同时我家里的电脑也安装了vs2019.So,就用vs2019+.NET ...

  2. Linux性能评估工具

    目录 介绍 负载:uptime 查看内核的信息: dmesg 查看内存状态: free.vmstat free: 查看内存,已用内存,剩余内存.交换分区等 vmstat:显示虚拟内存状况的信息. 查看 ...

  3. MyBridgeWebViewDemo【集成JsBridge开源库的的封装的webview】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用的是第三方库lzyzsd/JsBridge,但是不同的是,将自己封装的MyBridgeWebView通过继承BridgeWebV ...

  4. docker~swarm搭建docker高可用集群

    回到目录 Swarm概念 Swarm是Docker公司推出的用来管理docker集群,它将一群Docker宿主机变成一个单一的,虚拟的主机.Swarm使用标准的Docker API接口作为其前端访问入 ...

  5. ByteView和Sink

    久违啦米娜桑!!! 最近有点忙,一月有余没有更新了,实在对不起大家!!! 上线后看到不少朋友发的私信,感谢大家的赞许与信任,后面我会尽最大的努力按时更新,不断推出更优质的文章!!! 本来计划最后花1讲 ...

  6. webpack初探 - (一)

    什么webpack webpack是一个模块打包器.webpack把模块连同它的依赖一起打包生成包含这些模块的静态资源. 安装 在使用webpack时,建议不要把webpack安装到全局,如果多个项目 ...

  7. LeetCode 上最难的链表算法题,没有之一!

    题目来源于 LeetCode 第 23 号问题:合并 K 个排序链表. 该题在 LeetCode 官网上有关于链表的问题中标注为最难的一道题目:难度为 Hard ,通过率在链表 Hard 级别目前最低 ...

  8. sublime text3插件解决输入法不跟随的问题

    快捷键ctrl + shift +p 输入  install package 回车,调出插件搜索器, 在搜索栏中输入 IMESupport 回车安装插件. 即可解决问题.

  9. 001. Java内存中的字符编码

    Java内存中的字符编码 Unicode字符集及utf-8 .utf-16.utf-32 等字符编码方式 字符集:字符表示的数字集合,元素称为码点或码位: 字符编码:字符实际的储存表示: 码点:一个码 ...

  10. html meta标签使用及属性介绍

    自学前端开始,我对meta标签接触不多,主要把精力都集中在能显示出来的标签上,比如span.button.h1等等.有时候去查看一些知名网站的源码,发现head标签里有一大摞的meta. 今天就来学习 ...