各种格式的编码解码工具类分享(hex解码 base64编码)

所属分类: 软件编程 / java 阅读数: 89
收藏 0 赞 0 分享

复制代码 代码如下:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringEscapeUtils;

/**
 * 各种格式的编码加码工具类.
 *
 * 集成Commons-Codec,Commons-Lang及JDK提供的编解码方法.
 *
 * 
 */
public class EncodeUtils {

 private static final String DEFAULT_URL_ENCODING = "UTF-8";

 /**
  * Hex编码.
  */
 /*public static String hexEncode(byte[] input) {
  return Hex.encodeHexString(input);
 }*/

 /**
  * Hex解码.
  */
 public static byte[] hexDecode(String input) {
  try {
   return Hex.decodeHex(input.toCharArray());
  } catch (DecoderException e) {
   throw new IllegalStateException("Hex Decoder exception", e);
  }
 }

 /**
  * Base64编码.
  */
 public static String base64Encode(byte[] input) {
  return new String(Base64.encodeBase64(input));
 }

 /**
  * Base64编码, URL安全(将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548).
  */
 public static String base64UrlSafeEncode(byte[] input) {
  return Base64.encodeBase64URLSafeString(input);
 }

 /**
  * Base64解码.
  */
 public static byte[] base64Decode(String input) {
  return Base64.decodeBase64(input);
 }

 /**
  * URL 编码, Encode默认为UTF-8.
  */
 public static String urlEncode(String input) {
  try {
   return URLEncoder.encode(input, DEFAULT_URL_ENCODING);
  } catch (UnsupportedEncodingException e) {
   throw new IllegalArgumentException("Unsupported Encoding Exception", e);
  }
 }

 /**
  * URL 解码, Encode默认为UTF-8.
  */
 public static String urlDecode(String input) {
  try {
   return URLDecoder.decode(input, DEFAULT_URL_ENCODING);
  } catch (UnsupportedEncodingException e) {
   throw new IllegalArgumentException("Unsupported Encoding Exception", e);
  }
 }

 /**
  * Html 转码.
  */
 public static String htmlEscape(String html) {
  return StringEscapeUtils.escapeHtml(html);
 }

 /**
  * Html 解码.
  */
 public static String htmlUnescape(String htmlEscaped) {
  return StringEscapeUtils.unescapeHtml(htmlEscaped);
 }

 /**
  * Xml 转码.
  */
 public static String xmlEscape(String xml) {
  return StringEscapeUtils.escapeXml(xml);
 }

 /**
  * Xml 解码.
  */
 public static String xmlUnescape(String xmlEscaped) {
  return StringEscapeUtils.unescapeXml(xmlEscaped);
 }
}

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多