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< ...
随机推荐
- android学习---下拉刷新组建
Google官方的下拉刷新组建 activity代码实现: /** * The SwipeRefreshLayout should be used whenever the user * can re ...
- javaScript设计模式--观察者模式(observer)
观察者模式(observer):又被称为 发布-订阅者模式或者消息机制,定义了一种依赖关系,解决了主体对象与观察者之间功能耦合. 一.这样的需求 在实现自己的需求,而添加一些功能代码,但是又不想新添加 ...
- 《前端之路》之 JavaScript原型及原型链详解
05:JS 原型链 在 JavaScript 的世界中,万物皆对象! 但是这各种各样的对象其实具体来划分的话就 2 种. 一种是 函数对象,剩下的就是 普通对象.其中 Function 和 Objec ...
- 前端笔记之移动端&响应式(上)媒体查询&Bootstrap&动画库&zepto&velocity
一.媒体(介)查询 1.1 基本语法 媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于检测的媒体特性有:width.height和color(等).使用媒体查询可以在不改变 ...
- js事件循环机制辨析
对于新接触js语言的人来说,最令人困惑的大概就是事件循环机制了.最开始这也困惑了我好久,花了我几个月时间通过书本,打代码,查阅资料不停地渐进地理解他.接下来我想要和大家分享一下,虽然可能有些许错误的 ...
- python基础2--数据结构(列表List、元组Tuple、字典Dict)
1.Print函数中文编码问题 print中的编码:# -*- coding: utf-8 -*- 注:此处的#代表的是配置信息 print中的换行符,与C语言相同,为"\n" 2 ...
- AOP面向切面编程C#实例
原创: eleven 原文:https://mp.weixin.qq.com/s/8klfhCkagOxlF1R0qfZsgg [前言] AOP(Aspect-Oriented Programming ...
- [转]nodejs日期时间插件moment.js
本文转自:https://blog.csdn.net/dreamer2020/article/details/52278478 问题来源js自带的日期Date可以满足一些基本的需求,例如格式化.时间戳 ...
- vue element-ui 分页组件封装
<template> <el-pagination @size-change="handleSizeChange" @current-change="h ...
- WEB前端需要了解的XML相关基础知识
什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...