Base64是否有java版本的官方实现?
巴扎黑
巴扎黑 2017-04-18 10:04:55
0
5
525

写程序的时候需要用到base64的算法,自己实现时间不够,从网上找,代码无法信赖,所以想问下Base64编码算法是否有java版本的官方实现?或者是否有其他成熟的库实现了base64?

更新

很多同学提到了Apache Commons Codec,查资料看了下,感觉是当前找到的最好的解决方案了,多谢大家让我认识了这么好的库。

编码是开发中基础性的工作,有Codec这种专门解决编码问题的库,实在是方便了很多。建议由同样问题的同学尝试下Codec。

再更新

可以将codec做一个薄封装,简化工作流程,如下,供参考:

package apache.commons.codec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;

/**
 * Created by pengzhenhua on 2016/10/2.
 * 封装apache commons codec
 */
public class CodeC {

    private static Base64 base64 = new Base64();

    /**
     * 对字符串进行base64编码,返回编码结果
     * 默认先对字符串进行utf8解码,然后进行base64编码
     * @param str
     * @return
     */
    public static String base64(String str){
        return base64(str , Encode.UTF8);
    }

    /**
     * 使用指定的编码方式对字符串进行解码,然后进行base64编码
     * @param str
     * @param encode
     * @return
     */
    public static String base64(String str , Encode encode){
        try {
            str = base64.encodeToString(str.getBytes(encode.getName()));
        } catch (UnsupportedEncodingException e) {
            //just ignore
        }
        return str;
    }

    /**
     * base64解码
     * @param str
     * @return
     */
    public static String base64Decode(String str){
        return new String(Base64.decodeBase64(str));
    }

    /**
     * md5加密
     * 使用utf8解码字符串
     * @param str
     * @return
     */
    public static String md5(String str){
        return md5(str , Encode.UTF8);
    }

    /**
     * md5加密
     * 使用指定编码方式对字符串解码,然后md5加密
     * @param str
     * @param encode
     * @return
     */
    public static String md5(String str , Encode encode){
        try {
           str = new String(DigestUtils.md5Hex(str.getBytes(encode.getName())));
        } catch (UnsupportedEncodingException e) {
            //just ignore
        }
        return str;
    }

    enum Encode{
        UTF8("UTF8"),
        GBK("gbk")
        ;
        private String name;

        Encode(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
巴扎黑
巴扎黑

reply all(5)
刘奇

Same as above: commons-codec-1.10.jar is still very reliable;
Post a piece of code:


import java.io.UnsupportedEncodingException;  
import org.apache.commons.codec.binary.Base64;

public final class base64 {
          
        private static final String ENCODING = "ISO-8859-1";  
        /** 
         *  
         * @param data 
         * @return 
         * @throws UnsupportedEncodingException       
         * binaryData - Array containing binary data to encode. 
         * isChunked - if true this encoder will chunk the base64 output into 76 character blocks /r/n 
         * urlSafe - if true this encoder will emit - and _ instead of the usual + and / characters. Note: no padding is added when encoding using the URL-safe alphabet. 
         * maxResultSize - The maximum result size to accept. 
         */  
        public static String base64Encoder(String data) throws UnsupportedEncodingException{      
      
            byte [] binaryData = data.getBytes(ENCODING);  
            return Base64.encodeBase64String(binaryData);  
              
        }  
          
        public static String base64Decoder(String data) throws UnsupportedEncodingException{  
            byte [] binaryData = data.getBytes(ENCODING);  
            Base64.decodeBase64(data);  
            Base64.decodeInteger(binaryData);  
            return new String(Base64.decodeBase64(binaryData),ENCODING);  
        }  
}
洪涛

After jdk1.8, there is more javaUtil package java.util.Base64

大家讲道理

sun.misc.BASE64Decoder sun的实现。不过并不建议使用,因为是sunIn the bag.
BASE64 is an encoding, but it can be encoded/decoded normally and there will be no stability problems.

黄舟

You can use Apache Commons Codec
Note: Although it has not been updated since 2016, it is a mature library in itself

迷茫

Apache Commons projects can still be trusted. Apache Commons Codec.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!