各種格式的編碼解碼工具類分享(hex解碼 base64編碼)
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非法字符�?,/=轉(zhuǎn)為其他字符, 見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 轉(zhuǎn)碼.
*/
public static String htmlEscape(String html) {
return StringEscapeUtils.escapeHtml(html);
}
/**
* Html 解碼.
*/
public static String htmlUnescape(String htmlEscaped) {
return StringEscapeUtils.unescapeHtml(htmlEscaped);
}
/**
* Xml 轉(zhuǎn)碼.
*/
public static String xmlEscape(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 解碼.
*/
public static String xmlUnescape(String xmlEscaped) {
return StringEscapeUtils.unescapeXml(xmlEscaped);
}
}
相關(guān)文章
JAVA 實現(xiàn)二叉樹(鏈式存儲結(jié)構(gòu))
本篇文章主要介紹用JAVA 實現(xiàn)二叉樹,并提供實例.對二叉樹數(shù)據(jù)結(jié)構(gòu)很好的學習實踐,有需要的朋友可以參考下2016-07-07
深入理解Java中的構(gòu)造函數(shù)引用和方法引用
java構(gòu)造函數(shù),也叫構(gòu)造方法,是java中一種特殊的函數(shù)。函數(shù)名與相同,無返回值。方法引用是用來直接訪問類或者實例的已經(jīng)存在的方法或者構(gòu)造方法。下面我們來詳細了解一下它們吧2019-06-06
springboot3.0整合mybatis-flex實現(xiàn)逆向工程的示例代碼
逆向工程先創(chuàng)建數(shù)據(jù)庫表,由框架負責根據(jù)數(shù)據(jù)庫表,自動生成mybatis所要執(zhí)行的代碼,本文就來介紹一下springboot mybatis-flex逆向工程,感興趣的可以了解一下2024-06-06
Apache?Commons?Config管理配置文件核心功能使用
這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
java實現(xiàn)/創(chuàng)建線程的幾種方式小結(jié)
在JAVA中,用Thread類代表線程,所有線程對象都必須是Thread類或者Thread類子類的實例,下面這篇文章主要介紹了java實現(xiàn)/創(chuàng)建線程的幾種方式,需要的朋友可以參考下2021-08-08

