Apache组件commons的Codec包的使用实例

来源:互联网 发布:财政支出数据质量自查 编辑:程序博客网 时间:2024/04/29 08:01
base64特别适合在http,mime协议下快速传输数据。

base64编码 虽然叫做加密解密,编码后不是明文,其实这种是非常的简单的

下面是代码示例

package com.commons.codec;import java.io.UnsupportedEncodingException;import org.apache.commons.codec.binary.Base64;public class CodecTest {// 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);}public static void main(String[] args) {encodeTest("hello");decodeTest("hello");decodeTest("aGVsbG8=");}}
下面是显示结果:

Base64 编码后:aGVsbG8=Base64 解码后:呴eBase64 解码后:hello