java读取base64编码字符串并转成图片

时间:2023-01-11 12:14:41

    在网络中传输的图片,通常都是使用base64格式文件传输,本次读取的是保存在本地文件中的base64字符串,现在将其解析为原始图片。

import java.io.*;
import sun.misc.BASE64Decoder;

public class DailyCode {
    public static void main(String[] args) {
        String str;
        try {
            BufferedReader in = new BufferedReader(new FileReader("file/qr_bs64.txt"));
            while ((str=in.readLine()) != null) {
                break;
            }
        }catch (IOException e){
            System.out.println(e);
            str = "";
        }
        System.out.println(str);
        //对内容进行base64解码
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            FileOutputStream write = new FileOutputStream(new File("file/qr.png"));
            byte[] decodeBytes = decoder.decodeBuffer(str);
            write.write(decodeBytes);
            write.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

qrbs64.txt的内容如下:

iVBORw0KGgoAAAANSUhEUgAAARgAAAEYCAYAAACHjumMAAARXUlEQVR4nO2dwXEkuQ4Fx4MxpU1oU2SCTJEJMqVNkCljQv9j66Ct/1R8JF6BmRG4sggQzJ2NQFN/ngAAk/hTvQEA6AuCAYBpIBgAmAaCAYBpIBgAmAaCAYBpIBgAmAaCAYBpIBgAmAaCAYBpIBgAmAaCAYBpnBLMv3//nl9fX8/H40F8i6+vr+e/f/9sh6PW2f1d9/7SzyM9j6pw9NUpwXx9fT3f39+f9/ud+Bbv7+/Pr6+voQM5U2f3d937Sz+P9DyqwtFXpwTzeDye9/v9+efPH+Jb3O/35+PxGDqQM3V2f9e9v/TzSM8jvX5HIJiwAzlTZwQzVpf0PNLrdwSCCTuQM3VGMGN1Sc8jvX5HIJiwAzlTZwQzVpf0PNLrdwSCCTuQM3VGMGN1Sc8jvX5HIJiwAzlTZwQzVpf0PNLrdwSCCTuQM3VGMGN1Sc8jvX5HIJiwAzlTZwQzVpf0PNLrd8RUwfz9+/d5u92e94ChoZG43W7Pv3//Lr/o6gDY29vb8/Pz8/lYPMHpHlBT66z2lTooRj9fVDC32+358fFha/yq+Pj4eN5ut+WCUUfYPz8/n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