package com.opslab.util.web;

import com.opslab.util.ConvertUtil;
import com.opslab.util.StringUtil;

import java.io.UnsupportedEncodingException;

/**
* 提供Web相关的个工具类
*/
public final class WebUtil {

/**
* 对字符串进行编码
*
* @param str 需要处理的字符串
* @param encoding 编码方式
* @return 编码后的字符串
*/
public static String escape(String str, String encoding) throws UnsupportedEncodingException {
if (StringUtil.isEmpty(str)) {
return "";
}
char[] chars =ConvertUtil.bytesToChars(ConvertUtil.encodeBytes(str.getBytes(encoding), '%'));
return new String(chars);
}

/**
* 对字符串进行解码
*
* @param str 需要处理的字符串
* @param encoding 解码方式
* @return 解码后的字符串
*/
public static String unescape(String str,String encoding){
if(StringUtil.isEmpty(str)){
return "";
}
return UrlUtil.decodeQuery(str, encoding);
}

/**
* HTML标签转义方法
*
* 空格  
< 小于号 &lt;
> 大于号 &gt;
& 和号 &amp;
" 引号 &quot;
' 撇号 &apos;
¢ 分 &cent;
£ 镑 &pound;
¥ 日圆 &yen;
€ 欧元 &euro;
§ 小节 &sect;
© 版权 &copy;
® 注册商标 &reg;
™ 商标 &trade;
× 乘号 &times;
÷ 除号 &divide;
*/
public static String unhtml(String content) {
if (StringUtil.isEmpty(content)) {
return "";
}
String html = content;
html = html.replaceAll("'", "&apos;");
html = html.replaceAll("\"", "&quot;");
html = html.replaceAll("\t", "&nbsp;&nbsp;");// 替换跳格
html = html.replaceAll("<", "&lt;");
html = html.replaceAll(">", "&gt;");
return html;
}
public static String html(String content) {
if (StringUtil.isEmpty(content)) {
return "";
}
String html = content;
html = html.replaceAll("&apos;", "'");
html = html.replaceAll("&quot;", "\"");
html = html.replaceAll("&nbsp;", " ");// 替换跳格
html = html.replaceAll("&lt;", "<");
html = html.replaceAll("&gt;", ">");
return html;
}

}

最新文章

  1. 适合WebApi的简单的C#状态机实现
  2. Linux vi/vim(转载)
  3. CF735C 数论\平衡树叶子节点的最大深度\贪心\斐波那契\条件归一化
  4. matlab各格式数据读取与保存函数
  5. Sphinx中文分词详细安装配置及API调用实战
  6. Canvas与Image互转
  7. iOS应用架构谈 组件化方案
  8. android 多线程下载图片
  9. 【转】Android bluetooth介绍(二): android blueZ蓝牙代码架构及其uart 到rfcomm流程
  10. C语言循环的嵌套
  11. ExtJS简单的动画效果(ext js淡入淡出特效)
  12. C++单链表的创建与操作
  13. javascript 基础 onclick(this)用法介绍
  14. 从源码的角度看Service是如何启动的
  15. 逐步搭建Lamp环境之rpm软件包管理
  16. bzoj 1179: [Apio2009]Atm
  17. windows cmd下作MD5校验
  18. 流程帮App风险评估
  19. 使用uwsgi部署Django应用
  20. python-selenium,关于页面滑动的操作

热门文章

  1. 原生JavaScript和jQuery的较量
  2. 运行Vue项目,没办法自动打开浏览器,提示“Unable to open browser. If you are running in a headless environment, please do not use the open flag.”
  3. restful接口规范 | 基于restful的原生django接口
  4. [JSOI2018]战争
  5. LightOJ - 1311 - Unlucky Bird(相遇问题)
  6. CSS字体图标
  7. (23)打鸡儿教你Vue.js
  8. OpenStack Restful API框架介绍
  9. 【随记】Sql Server 2008 R2 备份时“无法打开备份设备”
  10. archlinux安装nvidia-1050ti闭源驱动教程,亲测