需要将由图片转成base64位的字符串转为图片的过程测试,定义一个String常量在进行测试解码的过程中,idea提示常量字符串过长
1 把过长的字符串放在某个文件中,然后通过读取文件获取里面的内容,也是本次尝试的处理方式,因为是测试就先这样处理
2 未尝试:更改idea的编译编辑器类型,具体可以查看文章: https://blog.csdn.net/qq_36236621/article/details/107997207
https://www.matools.com/image-base64
💡💡💡💡 这里的FileReader 用的hutool包中的类,导包的时候需要注意
@Test
public void encode() throws IOException {
FileReader fileReader = new FileReader("D:\\taet\\base.txt");
String resutl = fileReader.readString();
Base64Util.GenerateImage(resutl,"D:\\其他\\aa.jpg");
}
public class Base64Util {
public static void GenerateImage(String imgData,String imgFilePath) throws IOException{
if (imgData != null) {
// String imagedate = imgData.split(",")[1];
BASE64Decoder decoder = new BASE64Decoder();
OutputStream out = null;
try {
out = new FileOutputStream(imgFilePath);
// Base64解码
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
}
}
注意:这里在进行解析的时候需要去掉开头的部分"data:image/png;base64,"