BASE64的工具类

import java.nio.ByteBuffer;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * BASE64的工具类
 * @author kevinxu
 *
 */
public class BASE64Util
{   
    public BASE64Util()
    {  
    }
    
    /**
     * 按系统默认编码encode该字符串
     * @param s
     * @return String
     */
    public static String encode (String s)
    {
        return new BASE64Encoder().encode(s.getBytes());
    }
    
    /**
     * 对字节数组进行encode
     * @param bytes
     * @return String
     */
    public static String encode(byte[] bytes)
    {
        return new BASE64Encoder().encode(bytes);
    }
    
    /**
     * 对ByteBuffer进行encode
     * @param buf
     * @return String
     */
    public static String encode(ByteBuffer buf)
    {
        return new BASE64Encoder().encode(buf);
    }
    
    /**
     * 对BASE64的字符串进行decode,若decode失败,则返回null
     * @param str
     * @return byte[]
     */
    public static byte[] decode(String str)
    {
        try
        {
            return new BASE64Decoder().decodeBuffer(str);
        }
        catch(Exception e)
        {
            return null;
        }
    }
}


猜你喜欢

转载自blog.csdn.net/LC_Liangchao/article/details/121858114