Base64--图片base64解码简说

 Base64常用的就是把二进制数据编码成可打印的字符串

百科:Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。

Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,需要解码后才能阅读。

import java.util.Base64;

/**
 * Created by tian on 2017/3/29.
 */
public class Base64UtilTest1 {

    public static void main(String[] args) throws Exception {

        String source = "this is my source";
        System.out.println("源字符串:source:" + source);
        byte[] sourceByte = source.getBytes();
        System.out.println("源字符串字节数组(16进制):source.getBytes():" + HexUtil.byte2hex(sourceByte));
        byte[] encodeBase64Byte = Base64.getEncoder().encode(sourceByte);
        System.out.println("源字符串BASE64编码之后字节数组(16进制):HexUtil.byte2hex(encodeBase64Byte):" + HexUtil.byte2hex(encodeBase64Byte));
        String encodeBase64Str = new String(encodeBase64Byte);
        System.out.println("源字符串BASE64编码之后可打印字符串:new String(encodeBase64Str):" + encodeBase64Str);

        // 解码
        byte[] decodeBase64Byte = encodeBase64Str.getBytes();
        System.out.println("BASE64字符串解码前字节数组(16进制):HexUtil.byte2hex(decodeBase64Byte)" + HexUtil.byte2hex(decodeBase64Byte));
        byte[] decodeByte = Base64.getDecoder().decode(decodeBase64Byte);
        System.out.println("BASE64字符串解码后字节数组(16进制):HexUtil.byte2hex(decodeByte):" + HexUtil.byte2hex(decodeByte));
        String decodeStrFinal = new String(decodeByte);
        System.out.println("BASE64字符串解码后字符串:new String(decodeStrFinal):" + new String(decodeStrFinal));
    }
}

public class HexUtil {
    /**
     * 十六进制字符串转二进制
     *
     * @param hexStr 十六进制串
     * @return
     */
    public static byte[] hex2byte(String hexStr) { //字符串转二进制
        int len = hexStr.length();
        String stmp = null;
        byte bt[] = new byte[len / 2];
        for (int n = 0; n < len / 2; n++) {
            stmp = hexStr.substring(n * 2, n * 2 + 2);
            bt[n] = (byte) (Integer.parseInt(stmp, 16));
        }
        return bt;
    }

    /**
     * 二进制转十六进制字符串
     *
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) { //二行制转字符串
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
            if (n < b.length - 1) {
                hs = hs + "";
            }
        }
        return hs;
    }
}

 图片编码问题

项目中,发现部分图片的base64编码过后的图片二进制图片中节能有中间换行,导致base64解码失败,比如:

 

Base64图片文件可在浏览器打开,标签:<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ......></img>,即可打开。也可直接解码之后保存成文件:

    public static void main(String[] args) throws Exception {
            String frontIdCardStr = getFrontIdCardStr();
            // 去除字符串中的空格、回车、换行符、制表符
            frontIdCardStr = replaceBlank(frontIdCardStr); // 去调空格
            byte[] bytes = Base64Utils.decodeFromString(frontIdCardStr);
            File file = new File("D:\\frontIdCard.jpg");
            file.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
    }
            
    // 去除字符串中的空格、回车、换行符、制表符
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

 

猜你喜欢

转载自blog.csdn.net/szzt_lingpeng/article/details/82494913