在实际的应用中,我们经常需要对字符串进行编解码,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基本使用(转载)的更多相关文章

  1. ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android

    Andriod系统包中现在已经自带加密函数,如果用apache的codec包则会报以上错误,用android.util.Base64以下方法代替org.apache.commons.codec.bin ...

  2. Apache Commons Codec 编码解码

    Apache Commons Codec jar包官方下载地址 下载解压后把commons-codec-1.9.jar 放到lib中 关于SHA1算法的介绍可以参看Wiki:http://en.wik ...

  3. 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 ...

  4. 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 ...

  5. Apache Commons Codec的Base64加解密库

    下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi import org.apache.commons.cod ...

  6. java 调用apache.commons.codec的包简单实现MD5加密

    转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...

  7. Apache Commons Codec 与消息摘要算法(hash算法)

    首先我们要明白 Codec 是什么含义.它是 Coder + decoder = Codec,也就是编码器解码器.即是编码器,也是解码器. 官网地址:http://commons.apache.org ...

  8. md5加密(3)---org.apache.commons.codec.digest.DigestUtils.md5Hex(input)

    import org.apache.commons.codec.digest.DigestUtils;String sig = DigestUtils.md5Hex("str")

  9. 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在

    import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...

随机推荐

  1. <c:if>替代

    由于没有else, 由下面的替代 <c:choose> <c:when test="${usersession.hasPrivilegeByName('Case Delet ...

  2. 去掉input text后面的叉

    如题 input[type=text]::-ms-clear{ display: none; } input::-webkit-search-cancel-button{ display: none; ...

  3. 关于PHP静态方法调用和实例化类调用的区别

    1.首先来澄清一些观点 由于静态方法在内存中只有一份,无论你调用多少次,都是共用的,而且没有对象的概念,所以不能在静态方法里面使用$this调用,如果非得调用的话,只能实例化自身类 而实例化不一样,每 ...

  4. yum no key

    http://serverfault.com/questions/525958/redhat-yum-install-gpg-key-retrieval-failed

  5. 准备着手学习python

    1. python 的框架 Tornado 网址: http://www.tornadoweb.org/en/stable/ github: https://github.com/tornadoweb ...

  6. C++调用C#之C++DLL调用C# COM控件

    1. 新建项目 这里我们使用ATL,来接受C# COM控件向外发送的事件. 2. 初始化ATL #include "stdafx.h" CComModule _module; BO ...

  7. cell reuse & disposebag

    For my project I've made base cell class TableViewCell: UITableViewCell { private(set) var disposeBa ...

  8. IDAPython: importing “site” failed

    问题:IDA启动时,弹出IDAPython: importing “site” failed对话框. 解决办法:环境变量添加PYTHONHOME,值为python安装路径,比如:C:\Python27

  9. css05 字体以及行间距

    <head><meta charset="utf-8"><title>无标题文档</title><style>#box{ ...

  10. AndroidStudio使用注意事项

    今天在引入GitHUb上的开源框架时,写好依赖后编译时,报以下错误: Error:Execution failed for task ':app:processDebugResources'.> ...