我有一个base64编码的字符串,我从前端获得。现在我想将该图像字符串导出到图像文件,但它没有发生。在这里,你能告诉我我的错误吗?
dataURL
的格式为"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
。我已经使用了所有这三种方法,但我在所有这三种方法中都出现了错误。
public void ZSC_ExportDataURLasImage(String dataURL,String fileType,String filePath) throws IOException
{
// byte[] imagedata = DatatypeConverter.parseBase64Binary(dataURL.substring(dataURL.indexOf(",") + 1));
// byte[] imagedata = java.util.Base64.getDecoder().decode(dataURL.substring(dataURL.indexOf(",") + 1));
byte[] imagedata = Base64.decodeBase64(dataURL.substring(dataURL.indexOf(",") + 1));
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imagedata));
ImageIO.write(bufferedImage, fileType, new File(filePath));
}
我得到的错误是
Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at com.zscore.utils.ZSC_DataExporter.ZSC_ExportDataURLasImage(ZSC_DataExporter.java:20)
Caused by: java.util.zip.ZipException: invalid stored block lengths
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readFully(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(Unknown Source)
... 13 more