将 base 64 字符串保存为图像文件
Saving a base 64 String as image file
我有一个包含 base64 图像的 html 字符串,如下所示:
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABU1BMVEX////qQzU0qFNChfT7vAXb5/1blfVJivXL3Pw9gvTqPzBDh/W5z/vqQTMwp1DqPS7pOSn4/Pn/ /v7vQT97 7z vX 9/copUv2sq3rSz36/P/whHzz9/7tYFX4wLv 9PNIsWTn9Or 7sD5ysZRj/X//fXsUkWl1rHN6NR0wojzl5D61tP1rKZYt3Hq9u386Ob74d/yk4z 6K3 8s01pVz7xDM9kMH80mn8yUnE5My138GMy5z813f5sAvxcyKhwvk8la1BiOg/i9qCyJTo8P3wfnXvdGrual6v3Lqrx/phuXiQt/k6mpc jso4oH44noc2o2lyo/c9krg7mKCVsS5Xq0i1tCWOwWvfuRGnsyyGr/hyrT/Mtxv 67eY0KZruJ6ayMn934v71brtWh/3nBP8x0HtVi70jhrvbifyjmjrTzDygSD OX94Zn3pRF4rjvk6MKsrSR6AAAL2UlEQVR4nO2b63vaRhbGhYyTIEfifgsGQ0gEiIshl6ZAum4XAw5uu3W77WbThN1sm3a36yT7/3/akRDmJqQzI81IyqP3S54nNrZ PmfOe2bOiOMCBQoUKFCgQIECBQoUyHdKrOT2oziqxEnqtNuslnItRekVkHq9ntLKlarN7mkqlfE3bAahlVq98VTOh6LR2LqiUSEkFycFJYdIU77EPOlWc0phKiM0RCMihdal/oegfSlfnPRapeapvyhTzVxvUswLGlvIVBooCudYKXUzbj83UKmqotFZwm1iivK00Gp6HzLR7BXzorCdlDBKtDQnrVO3EcyUaSrFaAwjdruU0Vh XEq5DWKsxGlpnI9FieluKGPRYqt54jbOjjLd3ESICXbxFozRqNyreiuQmWZrKtoP34pR8BhjU5mGHOTTJAhyoeSRXO0qxZDgMF9IjSNirLoNh3TSKgoU DRGUZB7XZf5MqWiA XTBDImt9xcjolmwcn6YighOqm6thxTLTlGmS kekdecSdVE9WC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hi1QPKmNNyVnoXj9YuZM0UHlZVbzHJ qeKc/LFvMBK2VzaLl13HPAK2kXhkhh1QHxg2zqbg3VBmM2rzhbN4UTv3 9gh8fcNlVfrzhjrGBmFK2reVZ6N5393mBVPp mA mumYxqDaF1BGl9uNYa3v dw0UrpSH9SGGievj/H1cb4et/asMZxf9OsVLxZOqLR7JfX oDYfDkeNhUaj4bx2MejX65VK2o hM1Q8nlbV6XS0fxdXawIFChQoUKBAgQIFChQokCf0fy3M8V9F7y SAAAAAElFTkSuQmCC" alt="google.png" width="225" height="225" /></p>
我想将其保存到文件中。我只将 src 部分提取到 base64Content
中,当我 运行 以下代码时:
private void storeFile(String base64Content, String filename) {
try {
byte[] contentBytes = Base64.getMimeDecoder().decode(base64Content.split(",")[1]);
FileOutputStream imageOutFile = new FileOutputStream("/path/to/file/"+filename);
imageOutFile.write(contentBytes);
imageOutFile.close();
} catch (IOException ex) {
throw new FileStorageException("Could not store file " + filename + ". Please try again!", ex);
}
}
我收到以下错误:
java.lang.IllegalArgumentException: Last unit does not have enough valid bits
at java.util.Base64$Decoder.decode0(Base64.java:734) ~[na:1.8.0_181]
at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_181]
at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_181]
我该如何解决这个问题?
更新:
base64 字符串不正确,导致出现问题。更正后的 base64 字符串为
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABU1BMVEX////qQzU0qFNChfT7vAXb5/1blfVJivXL3Pw9gvTqPzBDh/W5z/vqQTMwp1DqPS7pOSn4/Pn/+/v7vQT97+7z+vX+9/copUv2sq3rSz36/P/whHzz9/7tYFX4wLv+9PNIsWTn9Or+7sD5ysZRj/X//fXsUkWl1rHN6NR0wojzl5D61tP1rKZYt3Hq9u386Ob74d/yk4z+6K3+8s01pVz7xDM9kMH80mn8yUnE5My138GMy5z813f5sAvxcyKhwvk8la1BiOg/i9qCyJTo8P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P0C8UnLT3/7DRTxhU1AjrsgIlSrans4gNbVTn8+u+HTPv3d97chjIQd6YbI8nQRifJs2LcOZBrhlTf41A8/+eEra0Tw9QtTVdqEUVQZF5HcvyRRco7a2vftfJb/i/ViBN6CshK+7288aTbLt0e1fqWTjiOpVKrSnUp9MG+U0Zf3/XRr2yBuubcUr5FYxiZkViq3Z43RfF5Dmg9HjVm7zJvQLRCRbZiG0ZEcVdUZ2QJcUCJl1yQZZebOh3hz23AmR1VVGvaCSCzVNqjnqKY6ebWxy7jXNiJHRIcz+zQA5BQlxH22Ybdd21bNvCjQRJQMbSNy5dgi1DW3WVBtKPvkMwNA+83MltJDFxH57d0G+KYljjouIqq2sQH44C75ycV+VRywRXLEb785WgN84fQiXCK6FkTVNr68sY2jMzqA+CeoziI++Vq3DYf6bUOlG66ZhtbgaLZBMA3FUNzFcqPtNo4iBzaORyFKz23tpWxKPaSimKI64oVrPSqvHuP9kzYgStQ+wSGqQ5LKNSYvCNTdKqmSzfN0uDojV0pqls2ta03xGs88jFJ2Rvum54bqM8a2IfEM7iNvqMLUNiRpxmoJrhTXxiiMAJ0boGMJhZFJxZGy5Qtmb5FtKt1vMAijxNsYSNpWp0Y7jFK27VYAdVWGPEVGKcsPmXqEoeqjMqVclaSyOxVmR30qjCof/RdWgEr3h22HGSWpDRk+slO6Pm9Dhi1QPKmNNyVnoXj9YuZM0UHlZVbzHJ+qeKc/LFvMBK2VzaLl13HPAK2kXhkhh1QHxg2zqbg3VBmM2rzhbN4UTv3+9gh8fcNlVfrzhjrGBmFK2reVZ6N5393mBVPp+mA+mumYxqDaF1BGl9uNYa3v+dw0UrpSH9SGGievj/H1cb4et/asMZxf9OsVLxZOqLR7JfX+oDYfDkeNhUaj4bx2MejX65VK2o+hM1Q8nlbV6XS0fxdXawIFChQoUKBAgQIFChQokCf0fy3M8V9F7y+SAAAAAElFTkSuQmCC
The implementation of this class supports the following types of
Base64 as specified in RFC 4648 and RFC 2045.
MIME
Uses the "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for
encoding and decoding operation.
The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately
by a linefeed '\n' as the line separator. No line separator is added
to the end of the encoded output. All line separators or other
characters not found in the base64 alphabet table are ignored in
decoding operation.
嗯,你的Base64spring显然不满足要求
最好像这样使用 javax.xml.bind.DatatypeConverter
:
byte[] contentBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Content.split(",")[1]);
我有一个包含 base64 图像的 html 字符串,如下所示:
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABU1BMVEX////qQzU0qFNChfT7vAXb5/1blfVJivXL3Pw9gvTqPzBDh/W5z/vqQTMwp1DqPS7pOSn4/Pn/ /v7vQT97 7z vX 9/copUv2sq3rSz36/P/whHzz9/7tYFX4wLv 9PNIsWTn9Or 7sD5ysZRj/X//fXsUkWl1rHN6NR0wojzl5D61tP1rKZYt3Hq9u386Ob74d/yk4z 6K3 8s01pVz7xDM9kMH80mn8yUnE5My138GMy5z813f5sAvxcyKhwvk8la1BiOg/i9qCyJTo8P3wfnXvdGrual6v3Lqrx/phuXiQt/k6mpc jso4oH44noc2o2lyo/c9krg7mKCVsS5Xq0i1tCWOwWvfuRGnsyyGr/hyrT/Mtxv 67eY0KZruJ6ayMn934v71brtWh/3nBP8x0HtVi70jhrvbifyjmjrTzDygSD OX94Zn3pRF4rjvk6MKsrSR6AAAL2UlEQVR4nO2b63vaRhbGhYyTIEfifgsGQ0gEiIshl6ZAum4XAw5uu3W77WbThN1sm3a36yT7/3/akRDmJqQzI81IyqP3S54nNrZ PmfOe2bOiOMCBQoUKFCgQIECBQoUyHdKrOT2oziqxEnqtNuslnItRekVkHq9ntLKlarN7mkqlfE3bAahlVq98VTOh6LR2LqiUSEkFycFJYdIU77EPOlWc0phKiM0RCMihdal/oegfSlfnPRapeapvyhTzVxvUswLGlvIVBooCudYKXUzbj83UKmqotFZwm1iivK00Gp6H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zYgStQ wSGqQ5LKNSYvCNTdKqmSzfN0uDojV0pqls2ta03xGs88jFJ2Rvum54bqM8a2IfEM7iNvqMLUNiRpxmoJrhTXxiiMAJ0boGMJhZFJxZGy5Qtmb5FtKt1vMAijxNsYSNpWp0Y7jFK27VYAdVWGPEVGKcsPmXqEoeqjMqVclaSyOxVmR30qjCof/RdWgEr3h22HGSWpDRk slO6Pm9Dhi1QPKmNNyVnoXj9YuZM0UHlZVbzHJ qeKc/LFvMBK2VzaLl13HPAK2kXhkhh1QHxg2zqbg3VBmM2rzhbN4UTv3 9gh8fcNlVfrzhjrGBmFK2reVZ6N5393mBVPp mA mumYxqDaF1BGl9uNYa3v dw0UrpSH9SGGievj/H1cb4et/asMZxf9OsVLxZOqLR7JfX oDYfDkeNhUaj4bx2MejX65VK2o hM1Q8nlbV6XS0fxdXawIFChQoUKBAgQIFChQokCf0fy3M8V9F7y SAAAAAElFTkSuQmCC" alt="google.png" width="225" height="225" /></p>
我想将其保存到文件中。我只将 src 部分提取到 base64Content
中,当我 运行 以下代码时:
private void storeFile(String base64Content, String filename) {
try {
byte[] contentBytes = Base64.getMimeDecoder().decode(base64Content.split(",")[1]);
FileOutputStream imageOutFile = new FileOutputStream("/path/to/file/"+filename);
imageOutFile.write(contentBytes);
imageOutFile.close();
} catch (IOException ex) {
throw new FileStorageException("Could not store file " + filename + ". Please try again!", ex);
}
}
我收到以下错误:
java.lang.IllegalArgumentException: Last unit does not have enough valid bits
at java.util.Base64$Decoder.decode0(Base64.java:734) ~[na:1.8.0_181]
at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_181]
at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_181]
我该如何解决这个问题?
更新: base64 字符串不正确,导致出现问题。更正后的 base64 字符串为
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABU1BMVEX////qQzU0qFNChfT7vAXb5/1blfVJivXL3Pw9gvTqPzBDh/W5z/vq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qDaF1BGl9uNYa3v+dw0UrpSH9SGGievj/H1cb4et/asMZxf9OsVLxZOqLR7JfX+oDYfDkeNhUaj4bx2MejX65VK2o+hM1Q8nlbV6XS0fxdXawIFChQoUKBAgQIFChQokCf0fy3M8V9F7y+SAAAAAElFTkSuQmCC
The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.
MIME
Uses the "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately by a linefeed '\n' as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.
嗯,你的Base64spring显然不满足要求
最好像这样使用 javax.xml.bind.DatatypeConverter
:
byte[] contentBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Content.split(",")[1]);