Commons Codec基本使用(转载)
在实际的应用中,我们经常需要对字符串进行编解码,Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic and URLs等等。
一、官方网址:
http://commons.apache.org/codec/
二、例子
1、 Base64编解码
private static String encodeTest(String str){
Base64 base64 = new Base64();
try {
str = base64.encodeToString(str.getBytes(“UTF-8”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(“Base64 编码后:”+str);
return str;
}
private static void decodeTest(String str){
Base64 base64 = new Base64();
//str = Arrays.toString(Base64.decodeBase64(str));
str = new String(Base64.decodeBase64(str));
System.out.println(“Base64 解码后:”+str);
}
2、 Hex编解码
private static String encodeHexTest(String str){
try {
str = Hex.encodeHexString(str.getBytes(“UTF-8”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(“Hex 编码后:”+str);
return str;
}
private static String decodeHexTest(String str){
Hex hex = new Hex();
try {
str = new String((byte[])hex.decode(str));
} catch (DecoderException e) {
e.printStackTrace();
}
System.out.println(“Hex 编码后:”+str);
return str;
}
3、 MD5加密
private static String MD5Test(String str){
try {
System.out.println(“MD5 编码后:”+newString(DigestUtils.md5Hex(str.getBytes(“UTF-8”))));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
4、 SHA编码
private static String ShaTest(String str){
try {
System.out.println(“SHA 编码后:”+newString(DigestUtils.shaHex(str.getBytes(“UTF-8”))));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
5、 Metaphone和Soundex
这个例子来源于网上,网址见:
http://350129923.blog.163.com/blog/static/17959113200763144659125/
Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发.
import org.apache.commons.codec.language.*;
import org.apache.commons.codec.*;
public class LanguageTest {
public static void main(String args[]) {
Metaphone metaphone = new Metaphone();
RefinedSoundex refinedSoundex = new RefinedSoundex();
Soundex soundex = new Soundex();
for (int i=0; i<2; i++ ) {
String str=(i==0)?”resume”:”resin”;
String mString = null;
String rString = null;
String sString = null;
try {
mString = metaphone.encode(str);
rString = refinedSoundex.encode(str);
sString = soundex.encode(str);
} catch (Exception ex) {
;
}
System.out.println(“Original:”+str);
System.out.println(“Metaphone:”+mString);
System.out.println(“RefinedSoundex:”+rString);
System.out.println(“Soundex:”+sString +”\\n”);
}
}
}
Commons Codec基本使用(转载)的更多相关文章
- ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android
Andriod系统包中现在已经自带加密函数,如果用apache的codec包则会报以上错误,用android.util.Base64以下方法代替org.apache.commons.codec.bin ...
- Apache Commons Codec 编码解码
Apache Commons Codec jar包官方下载地址 下载解压后把commons-codec-1.9.jar 放到lib中 关于SHA1算法的介绍可以参看Wiki:http://en.wik ...
- Java之加密(信息摘要)工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)
依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256 import java.security.MessageDigest; import java.s ...
- Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.codec.digest.DigestUtils.sha1Hex(Ljava/lang/String;)Ljava/lang/String;
异常:Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.apache.commons.co ...
- Apache Commons Codec的Base64加解密库
下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi import org.apache.commons.cod ...
- java 调用apache.commons.codec的包简单实现MD5加密
转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...
- Apache Commons Codec 与消息摘要算法(hash算法)
首先我们要明白 Codec 是什么含义.它是 Coder + decoder = Codec,也就是编码器解码器.即是编码器,也是解码器. 官网地址:http://commons.apache.org ...
- md5加密(3)---org.apache.commons.codec.digest.DigestUtils.md5Hex(input)
import org.apache.commons.codec.digest.DigestUtils;String sig = DigestUtils.md5Hex("str")
- 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在
import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...
随机推荐
- ice使用过程遇到的问题
1 设置代理超时时间ice_timeout ICE的每个连接都有两个超时时间:ice_timeout.ice_connectiontimeout,分别对应消息的超时时间和连接建立 的超时时间,可 ...
- 访问nginx-php页面的时候 报access denied.
访问页面的时候出现这个时access denied 只需到/usr/local/php/etc/php.ini中修改一下 把这个注释掉 ;open_basedir = 把这个值赋值为1 cgi.f ...
- rndc 错误解决 和 远程配置
dc: connect failed: connection refusedrndc: connect failed: connection refused 解决办法:默认安装BIND9以后,是无法直 ...
- CodeForces 510B DFS水题
题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<c ...
- Apache + PHP in Windows XP (to add SQLite)
Firstly, Winxp do not support VC11+, so choose Apache packs compiled under VC10-. Before installatio ...
- List、Set、Map集合存放null解析及HashMap、Hashtable异同点解析
1.List.Set.Map集合存放null解析: @Test public void CollectionTest() { // 测试List List<Object> list = n ...
- JS-运动基础(一)
<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red; posit ...
- HDU1171--Big Event in HDU(多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- java 网络编程Socket编程
Server.java import java.io.*; import java.net.*; public class Server { public static void main(Strin ...
- android 学习之RecyclerView
RecyclerView:ListView的升级版,它提供了更好的性能而且更容易使用.该控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动.当你list中的元素经常动态改变时可以使用R ...