Java 将 Base64 编码的图像写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20879639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write Base64-encoded image to file
提问by DCoder
How to write a Base64-encoded image to file?
如何将 Base64 编码的图像写入文件?
I have encoded an image to a string using Base64. First, I read the file, then convert it to a byte array and then apply Base64 encoding to convert the image to a string.
我使用 Base64 将图像编码为字符串。首先,我读取文件,然后将其转换为字节数组,然后应用 Base64 编码将图像转换为字符串。
Now my problem is how to decode it.
现在我的问题是如何解码它。
byte dearr[] = Base64.decodeBase64(crntImage);
File outF = new File("c:/decode/abc.bmp");
BufferedImage img02 = ImageIO.write(img02, "bmp", outF);
The variable crntImagecontains the string representation of the image.
该变量crntImage包含图像的字符串表示。
采纳答案by Jon Skeet
Assuming the image data is already in the format you want, you don't need image ImageIOat all - you just need to write the data to the file:
假设图像数据已经是你想要的格式,你根本不需要图像ImageIO——你只需要将数据写入文件:
// Note preferred way of declaring an array variable
byte[] data = Base64.decodeBase64(crntImage);
try (OutputStream stream = new FileOutputStream("c:/decode/abc.bmp")) {
stream.write(data);
}
(I'm assuming you're using Java 7 here - if not, you'll need to write a manual try/finally statement to close the stream.)
(我假设您在这里使用 Java 7 - 如果没有,您将需要编写手动 try/finally 语句来关闭流。)
If the image data isn'tin the format you want, you'll need to give more details.
如果图像数据不是您想要的格式,您需要提供更多详细信息。
回答by Jamel ESSOUSSI
Try this:
尝试这个:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class WriteImage
{
public static void main( String[] args )
{
BufferedImage image = null;
try {
URL url = new URL("URL_IMAGE");
image = ImageIO.read(url);
ImageIO.write(image, "jpg",new File("C:\out.jpg"));
ImageIO.write(image, "gif",new File("C:\out.gif"));
ImageIO.write(image, "png",new File("C:\out.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}
回答by KaviK
No need to use BufferedImage, as you already have the image file in a byte array
无需使用 BufferedImage,因为您已经在字节数组中拥有图像文件
byte dearr[] = Base64.decodeBase64(crntImage);
FileOutputStream fos = new FileOutputStream(new File("c:/decode/abc.bmp"));
fos.write(dearr);
fos.close();
回答by Fabio De Carli
Other option using apache-commons:
使用 apache-commons 的其他选项:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
...
File file = new File( "path" );
byte[] bytes = Base64.decodeBase64( "base64" );
FileUtils.writeByteArrayToFile( file, bytes );
回答by Matthias Braun
With Java 8's Base64API
使用 Java 8 的Base64API
byte[] decodedImg = Base64.getDecoder()
.decode(encodedImg.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("/path/to/imageDir", "myImage.jpg");
Files.write(destinationFile, decodedImg);
If your encoded image starts with something like data:image/png;base64,iVBORw0..., you'll have to remove the part. See this answerfor an easy way to do that.
如果您的编码图像以 开头,data:image/png;base64,iVBORw0...则必须删除该部分。请参阅此答案以了解一种简单的方法。
回答by DAB
import java.util.Base64;
.... Just making it clear that this answer uses the java.util.Base64 package, without using any third-party libraries.
.... 只是明确说明这个答案使用 java.util.Base64 包,而不使用任何第三方库。
String crntImage=<a valid base 64 string>
byte[] data = Base64.getDecoder().decode(crntImage);
try( OutputStream stream = new FileOutputStream("d:/temp/abc.pdf") )
{
stream.write(data);
}
catch (Exception e)
{
System.err.println("Couldn't write to file...");
}

