Base64 编解码 工具类

转载自:http://wuhaocn.iteye.com/blog/2231008 如要转载请注明文章出处

1.工具类:

Java代码  

  1. import java.io.UnsupportedEncodingException;  
  2. import java.util.Base64;  
  3.   
  4. public class Base64Util {  
  5.     /** 
  6.      * base64 编码 
  7.      *  
  8.      * @param str 
  9.      * @return 
  10.      */  
  11.     public static String encoder(String str){  
  12.         final Base64.Encoder encoder = Base64.getEncoder();  
  13.         String strEncoder = null;  
  14.         try {  
  15.             strEncoder = encoder.encodeToString(str.getBytes("UTF-8"));  
  16.         } catch (UnsupportedEncodingException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return strEncoder;  
  20.     }  
  21.     /** 
  22.      * base64 解码 
  23.      *  
  24.      * @param str 
  25.      * @return 
  26.      */  
  27.     public static String decoder(String str){  
  28.         final Base64.Decoder decoder = Base64.getDecoder();  
  29.         String strEncoder = null;  
  30.         try {  
  31.             strEncoder = new String(decoder.decode(str), "UTF-8");  
  32.         } catch (UnsupportedEncodingException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         return strEncoder;  
  36.     }  
  37.       
  38.     /** 
  39.      * test 
  40.      *  
  41.      * @param args 
  42.      */  
  43.     public static void main(String[] args){  
  44.         String testStr = "qwerqwfavavaegfgbsd#*#-asdfasd-asdf-fasdf-asd";  
  45.         String encoderStr = encoder(testStr);  
  46.         System.out.println(encoderStr);  
  47.         String decoderStr = decoder(encoderStr);  
  48.         System.out.println(decoderStr);  
  49.     }  
  50. }  
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Util {
	/**
	 * base64 编码
	 * 
	 * @param str
	 * @return
	 */
	public static String encoder(String str){
		final Base64.Encoder encoder = Base64.getEncoder();
		String strEncoder = null;
		try {
			strEncoder = encoder.encodeToString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strEncoder;
	}
	/**
	 * base64 解码
	 * 
	 * @param str
	 * @return
	 */
	public static String decoder(String str){
		final Base64.Decoder decoder = Base64.getDecoder();
		String strEncoder = null;
		try {
			strEncoder = new String(decoder.decode(str), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strEncoder;
	}
	
	/**
	 * test
	 * 
	 * @param args
	 */
	public static void main(String[] args){
		String testStr = "qwerqwfavavaegfgbsd#*#-asdfasd-asdf-fasdf-asd";
		String encoderStr = encoder(testStr);
		System.out.println(encoderStr);
		String decoderStr = decoder(encoderStr);
		System.out.println(decoderStr);
	}
}



2.备考
Java上的Base64
早期作法
早期在Java上做Base64的編碼與解碼,會使用到JDK裡sun.misc套件下的BASE64Encoder和BASE64Decoder這兩個類別,用法如下:

final BASE64Encoder encoder = new BASE64Encoder();
final BASE64Decoder decoder = new BASE64Decoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = encoder.encode(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
從以上程式可以發現,在Java用Base64一點都不難,不用幾行程式碼就解決了!只是這個sun.misc套件所提供的Base64功能,編碼和解碼的效率並不太好,而且在以後的Java版本可能就不被支援了,完全不建議使用。

Apache Commons Codec作法
Apache Commons Codec有提供Base64的編碼與解碼功能,會使用到org.apache.commons.codec.binary套件下的Base64類別,用法如下:

final Base64 base64 = new Base64();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = base64.encodeToString(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(base64.decode(encodedText), "UTF-8"));
以上的程式碼看起來又比早期用sun.misc套件還要更精簡,效能實際執行起來也快了不少。缺點是需要引用Apache Commons Codec,很麻煩。

Java 8之後的作法
Java 8的java.util套件中,新增了Base64的類別,可以用來處理Base64的編碼與解碼,用法如下:

final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//編碼
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解碼
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
與sun.misc套件和Apache Commons Codec所提供的Base64編解碼器來比較的話,Java 8提供的Base64擁有更好的效能。實際測試編碼與解碼速度的話,Java 8提供的Base64,要比sun.misc套件提供的還要快至少11倍,比Apache Commons Codec提供的還要快至少3倍。因此在Java上若要使用Base64,這個Java 8底下的java.util套件所提供的Base64類別絕對是首選!

原文链接:http://magiclen.org/java-base64/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值