【Java】+ 图片与base64互转 + 字符串与base64互转

package team.giat.branch.api_risignhf.testcase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

/**
 * Author: Jarvis
 * Date: 2020/4/15 10:11
 * Version:v1.0.0
 */
public class TestBase64 {

    /**
     * 功能:图片转为Base64字节码
     *
     * @param path 图片路径
     * @return 返回base64字节码
     */
    public static String imageToBase64(String path) {
        byte[] data = null;
        try {
            InputStream in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64 base64 = new Base64();
        return base64.encodeToString(data);
    }

    /**
     * 功能:Base64字节码转图片
     *
     * @param base64Str 字节码存储路径
     * @param path      文件存储路径
     * @return 返回true或者false
     */
    public static boolean base64ToImage(String base64Str, String path) {
        if (base64Str == null) {
            return false;
        }
        Base64 base64 = new Base64();
        try {
            byte[] bytes = base64.decodeBase64(base64Str);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            File img = new File(path);
            if (!img.getParentFile().exists()) {
                img.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(path);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 功能:字符串转Base64字节码
     *
     * @param str 字符串
     * @return 返回经Base64加密后的字节码字符串
     */
    public static String strToBase64(String str) {
        byte[] bytes = Base64.encodeBase64(str.getBytes());
        return new String(bytes);
    }

    /**
     * 功能:Base64字节码转字符串
     *
     * @param base64Str Base64字节码字符串
     * @return 返回经base64解码后的原始字符串
     */
    public static String base64ToStr(String base64Str) {
        byte[] bytes = Base64.decodeBase64(base64Str);
        return new String(bytes);
    }

    public static void main(String[] args) {
        // -------------------图片
        // 加密
        String s = imageToBase64("C:\\Users\\admin\\Pictures\\Camera Roll\\1.jpg");
        System.out.println(String.format("图片加密后:%s", s));

        // 解密
        String ss = s;
//        String ss = "qlUZAQWM3Id7cQEAAAEBAgQCBAIEAgQCBAE+VQ==";
        boolean b = base64ToImage(ss, ".\\aaaaaaaaaa.jpg");
        System.out.println(String.format("图片解密结果:%s", b));

        // -------------------字符串
        // 加密
        String base64Str = strToBase64("abcd中文");
        System.out.println(String.format("字符串加密后:%s", base64Str));

        // 解密
        String base64Str2 = base64Str;
//        String base64Str2 = "qlUZAQWM3Id7cQEAAAEBAgQCBAIEAgQCBAE+VQ==";
        String oldStr = base64ToStr(base64Str2);
        System.out.println(String.format("字符串解密后:%s", oldStr));

    }

}

 

posted @ 2020-04-15 10:51  淡怀  阅读(388)  评论(0编辑  收藏  举报