当前位置: 首页 > 工具软件 > Java Base64 > 使用案例 >

java.util.base64

臧亦
2023-12-01
package util;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class MyBase64 {

    /**
     * base64加密方法
     * 
     * Create by 暴沸 2016年5月24日  at 下午8:21:20 
     *
     * Mailto:baofei_dyz@foxmail.com
     *
     * @param plainText
     * @return
     */
    public static String getEncodedBase64(String plainText){
        String encoded = null;
        try {
            byte[] bytes =plainText.getBytes("UTF-8");
            encoded = Base64.getEncoder().encodeToString(bytes);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return encoded;
    }

    /**
     * base64解密方法
     * 
     * Create by 暴沸 2016年5月24日  at 下午8:21:02 
     *
     * Mailto:baofei_dyz@foxmail.com
     *
     * @param plainText
     * @return
     */
    public static byte[] getDecodedBase64(String plainText){
        byte[] decoded = null;
        try {
            byte[] bytes =plainText.getBytes("UTF-8");
            decoded = Base64.getDecoder().decode(bytes);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return decoded;
    }
}
private byte[] InputStream2ByteArray(String filePath) throws IOException {
 
    InputStream in = new FileInputStream(filePath);
    byte[] data = toByteArray(in);
    in.close();
 
    return data;
}
 
private byte[] toByteArray(InputStream in) throws IOException {
 
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 4];
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
        out.write(buffer, 0, n);
    }
    return out.toByteArray();
}


 类似资料:

相关阅读

相关文章

相关问答