各種格式的編碼解碼工具類分享(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默認(rèn)為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默認(rèn)為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設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式
這篇文章主要介紹了淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式,使用設(shè)計(jì)模式編寫代碼有利于團(tuán)隊(duì)協(xié)作時(shí)程序的維護(hù),需要的朋友可以參考下2016-01-01
JAVA 實(shí)現(xiàn)二叉樹(鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu))
本篇文章主要介紹用JAVA 實(shí)現(xiàn)二叉樹,并提供實(shí)例.對(duì)二叉樹數(shù)據(jù)結(jié)構(gòu)很好的學(xué)習(xí)實(shí)踐,有需要的朋友可以參考下2016-07-07
深入理解Java中的構(gòu)造函數(shù)引用和方法引用
java構(gòu)造函數(shù),也叫構(gòu)造方法,是java中一種特殊的函數(shù)。函數(shù)名與相同,無(wú)返回值。方法引用是用來(lái)直接訪問(wèn)類或者實(shí)例的已經(jīng)存在的方法或者構(gòu)造方法。下面我們來(lái)詳細(xì)了解一下它們吧2019-06-06
java省市級(jí)聯(lián)下拉菜單實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了java省市級(jí)聯(lián)實(shí)例代碼,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-05-05
springboot3.0整合mybatis-flex實(shí)現(xiàn)逆向工程的示例代碼
逆向工程先創(chuàng)建數(shù)據(jù)庫(kù)表,由框架負(fù)責(zé)根據(jù)數(shù)據(jù)庫(kù)表,自動(dòng)生成mybatis所要執(zhí)行的代碼,本文就來(lái)介紹一下springboot mybatis-flex逆向工程,感興趣的可以了解一下2024-06-06
Apache?Commons?Config管理配置文件核心功能使用
這篇文章主要為大家介紹了Apache?Commons?Config管理和使用配置文件核心深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java 數(shù)據(jù)庫(kù)連接池 DBCP 的介紹
這篇文章主要給大家分享的是 Java 數(shù)據(jù)庫(kù)連接池 DBCP 的介紹, 是 Apache 旗下 Commons 項(xiàng)目下的一個(gè)子項(xiàng)目,提供連接池功能DBCP,下面來(lái)看看文章的具體介紹內(nèi)容吧,需要的朋友可以參考一下2021-11-11
java實(shí)現(xiàn)/創(chuàng)建線程的幾種方式小結(jié)
在JAVA中,用Thread類代表線程,所有線程對(duì)象都必須是Thread類或者Thread類子類的實(shí)例,下面這篇文章主要介紹了java實(shí)現(xiàn)/創(chuàng)建線程的幾種方式,需要的朋友可以參考下2021-08-08

