commons-codec常用方法

一.maven添加依赖

    commons-codec是Apache开源组织提供的用于摘要运算、编码解码的包。常见的编码解码工具Base64、MD5、Hex、SHA1、DES等。

    api:http://tool.oschina.net/apidocs/apidoc?api=commons-codec

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.11</version>
    </dependency>

二.应用实例

1、Base64编码和解码
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.Base64;
public class TestBase64 {
    public static void main(String[] args) throws EncoderException, UnsupportedEncodingException {
        Base64 base64 = new Base64();
        String str = "hellowBase64";
        String result = base64.encodeToString(str.getBytes("UTF-8"));//编码
        System.out.println(result);
        byte[] decode = base64.decode(result.getBytes());//解码
        System.out.println(new String(decode));
    }
}
2、Hex编码和解码
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public class TestHex {
    public static void main(String[] args) throws DecoderException, UnsupportedEncodingException {
        String str = "test";
        /**编码*/
        String hexString = Hex.encodeHexString(str.getBytes("UTF-8"));//直接一步到位
        System.out.println(hexString);
        char[] encodeHex = Hex.encodeHex(str.getBytes(), true);//先转换成char数组,第二个参数意思是是否全部转换成小写
        System.out.println(new String(encodeHex));
        /**解码*/
        byte[] decodeHex = Hex.decodeHex(encodeHex);//char数组型的
        System.out.println(new String(decodeHex));
        byte[] decodeHex2 = Hex.decodeHex(hexString.toCharArray());//字符串类型的,该方法要求传入的是char[]
        System.out.println(new String(decodeHex2));
    }
}
3、MD5加密(MD5是不可逆算法,只能加密)
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestMD5 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test";
        String md5 = DigestUtils.md5Hex(str.getBytes("UTF-8"));
        System.out.println(md5);
    }
}
4、SHA加密
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.digest.DigestUtils;
public class TestSHA {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "test开源中国";
        String sha1Hex = DigestUtils.sha1Hex(str.getBytes("UTF-8"));
        System.out.println(sha1Hex);
    }
}
5、Metaphone和Soundex
Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对, 也可以用在 MP3 的软件开发
metaphone() 比 soundex() 函数更精确,因为 metaphone() 了解基本的英语发音规则

import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
public class TestMetaphoneAndSoundex {
    public static void main(String[] args) {
        String str = "testgggggg";
        /**Metaphone没有固定长度*/
        Metaphone metaphone = new Metaphone();
        String metaphoneEncode = metaphone.encode(str);
        System.out.println(metaphoneEncode);
        /**RefinedSoundex*/
        RefinedSoundex refinedSoundex = new RefinedSoundex();
        String refinedSoundexEncode = refinedSoundex.encode(str);
        System.out.println(refinedSoundexEncode);
        /**Soundex固定第一个英文字加上3个数字*/
        Soundex soundex = new Soundex();
        String soundexEncode = soundex.encode(str);
        System.out.println(soundexEncode);
    }
}
6、URLCodec
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
public class TestURLCodec {
    public static void main(String[] args) throws EncoderException, DecoderException {
        String url = "https://www.oschina.net/?name=你好";
        URLCodec codec = new URLCodec();
        String encode = codec.encode(url);
        System.out.println(encode);
        String decode = codec.decode(encode);
        System.out.println(decode);
    }
}

转载于:https://my.oschina.net/warm6Y/blog/2991786

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值