在网络中传输的图片,通常都是使用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/TzSM8jvX5HIJiwAzlTZwQz