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. 架构师系列文:通过Spring Cloud组件Hystrix合并请求

    在前文里,我们讲述了通过Hystrix进行容错处理的方式,这里我们将讲述通过Hystrix合并请求的方式 哪怕一个URL请求调用的功能再简单,Web应用服务都至少会开启一个线程来提供服务,换句话说,有 ...

  2. RecyclerFullyManagerDemo【ScrollView里嵌套Recycleview的自适应高度功能】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 对于Recyclerview自己的LinearLayoutManager和GridLayoutManager,在版本23.2.0之后 ...

  3. Flink中的Time

    戳更多文章: 1-Flink入门 2-本地环境搭建&构建第一个Flink应用 3-DataSet API 4-DataSteam API 5-集群部署 6-分布式缓存 7-重启策略 8-Fli ...

  4. 一套代码小程序&Web&Native运行的探索05——snabbdom

    接上文:一套代码小程序&Web&Native运行的探索04——数据更新 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/ma ...

  5. Asp.Net Core 轻松学-项目目录和文件作用介绍

    前言     上一章介绍了 Asp.Net Core 的前世今生,并创建了一个控制台项目编译并运行成功,本章的内容介绍 .NETCore 的各种常用命令.Asp.Net Core MVC 项目文件目录 ...

  6. SmartSql 常见问题

    常见问题 为什么不支持 Linq? SmartSql 希望 开发人员更多的接触 Sql ,获得绝对的控制权与安全感.所以目前没有计划支持 Code First 编程模式. 我想好了Sql怎么写,然后再 ...

  7. .Net Framework项目引用.NetStandard标准库出现版本冲突解决办法

    今天在工作中出现一个引用问题,害我找问题找了很久.起因是在一个Winform项目下需要引用一个.NetStandard标准库,标准库引用了System.ComponentModel.Annotatio ...

  8. java实现 批量转换文件编码格式

    一.场景说明 不知道大家有没有遇到过之前项目是GBK,现在需要全部换成UTF-8的情况.反正我是遇到了. eclipse可以改变项目的编码格式,但是文件如果直接转换的话里面的中文就会全部乱码,需要先复 ...

  9. No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"

    安装完NDK的时候出现了这个错误,网上的办法是下载旧版的NDK,将其中的toolchain复制到新版的NDK中. 但其实不用这么麻烦. 经过对新版NDK的研究,发现NDK的更新记录里有一段话 This ...

  10. Android launcher 壁纸 wallpaper

    壁纸分为动态和静态两种: 如果只需要修改默认静态壁纸,替换frameworks/base/core/res/res/drawable/default_wallpaper.jpg即可,或者在源码中修改对 ...