Base64转换

Base64转换

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

import java.io.*;

/**
 * 文件Base64转换
 * Created by 张勇波 on 2017/5/8.
 */
public class Base64Convert {
    /**
     * 将文件转成base64字符串
     *
     * @param path 文件路径
     * @return base64 字符串
     */
    public static String encodeBase64File(String path) {
        File file = new File(path);
        FileInputStream inputFile;
        byte[] buffer = new byte[0];
        try {
            inputFile = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BASE64Encoder().encode(buffer);
    }

    /**
     * 将base64字符解码保存文件
     *
     * @param base64Code base64字符串
     * @param targetPath 保存文件地址
     */
    public static void decoderBase64File(String base64Code, String targetPath) {
        try {
            byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
            FileOutputStream out = new FileOutputStream(targetPath);
            out.write(buffer);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将base64字符保存物理文件
     *
     * @param base64Code base64字符串
     * @param targetPath 文件地址(包括文件名和后缀)
     * @param decode     base64字符串编码,默认 uft8
     */
    public static void toFile(String base64Code, String targetPath, String decode) {
        if (base64Code != null && base64Code.length() > 0) {
            decode = decode == null ? "utf-8" : decode;
            byte[] buffer;
            try {
                buffer = base64Code.getBytes(decode);
                FileOutputStream out = new FileOutputStream(targetPath);
                out.write(buffer);
                out.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将字符串进行Base64转码
     *
     * @param str    待转码的字符串
     * @param decode 编码格式,默认 utf-8
     * @return 转码后的 Base64字符串
     */
    public static String encodeBase64String(String str, String decode) {
        byte[] b = null;
        String s = null;
        decode = decode == null ? "utf-8" : decode;
        try {
            b = str.getBytes(decode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return s;
    }

    /**
     * 将Base64字符串解码
     *
     * @param s      待解码的Base64字符串
     * @param decode 待解码的Base64字符串的编码格式,默认为 utf-8
     * @return 解码后的字符串
     */
    public static String decoderBase64String(String s, String decode) {
        byte[] b;
        decode = decode == null ? "utf-8" : decode;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, decode);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        //try {
        //    String base64Code = encodeBase64File("/Users/ZYB/Downloads/dao_black.png");
        //    System.out.println(base64Code);
        //    decoderBase64File(base64Code, "/Users/ZYB/Downloads/dao_black2.png");
        //    toFile(base64Code, "/Users/ZYB/Downloads/dao_black.txt");
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}
    }
}

  目录