【问题标题】:Write Base64-encoded image to file将 Base64 编码的图像写入文件
【发布时间】:2014-01-19 16:56:07
【问题描述】:

如何将 Base64 编码的图像写入文件?

我已使用 Base64 将图像编码为字符串。 首先,我读取文件,然后将其转换为字节数组,然后应用 Base64 编码将图像转换为字符串。

现在我的问题是如何解码。

byte dearr[] = Base64.decodeBase64(crntImage);
File outF = new File("c:/decode/abc.bmp");
BufferedImage img02 = ImageIO.write(img02, "bmp", outF); 

变量crntImage 包含图像的字符串表示形式。

【问题讨论】:

    标签: java image base64 decode encode


    【解决方案1】:

    假设图像数据已经是你想要的格式,你根本不需要图像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);
    }
    

    (我假设您在这里使用的是 Java 7 - 如果没有,您将需要编写手动 try/finally 语句来关闭流。)

    如果图像数据不是您想要的格式,您需要提供更多详细信息。

    【讨论】:

    • 我使用了你上面的代码。我正在开发 java web 服务,我正在获取上传图像形式的 IOS 设备的 base64 字符串。当我在单独的应用程序中尝试您的上述代码时,我得到了原始图像。但是当我尝试使用 Web 服务时,没有创建图像。当我在这两种情况下调试应用程序时,相同base64字符串的字节数组长度不同。为什么我会遇到这个问题。
    • @Aniket:这并没有为我们提供几乎足够的信息来帮助您。我建议你问一个新的问题,包括更多的上下文——网络服务是什么,它是如何实现的,你如何上传图像数据,你观察到了什么等等。
    • 我在下面的链接stackoverflow.com/questions/27378069/…发布了问题
    • "我假设您使用的是 Java 7" => 您的意思是 Java 8,而不是 Java 7。Afaik Base64 和 try-with-resources 都是 Java 8。
    • @Toto:不,try-with-resources 肯定是在 Java 7 中:docs.oracle.com/javase/7/docs/technotes/guides/language/…Java 8 在我写这个答案时没有发布,尽管我猜它在 beta 中可用。但是 OP 使用的 Base64 类也不是 JDK 中的类。 (它没有decodeBase64 方法。)
    【解决方案2】:

    使用 Java 8 的 Base64 API

    byte[] decodedImg = Base64.getDecoder()
                        .decode(encodedImg.getBytes(StandardCharsets.UTF_8));
    Path destinationFile = Paths.get("/path/to/imageDir", "myImage.jpg");
    Files.write(destinationFile, decodedImg);
    

    如果您的编码图像以 data:image/png;base64,iVBORw0... 之类的开头,则必须删除该部分。请参阅this answer,了解一种简单的方法。

    【讨论】:

      【解决方案3】:

      无需使用 BufferedImage,因为您已经将图像文件保存在字节数组中

          byte dearr[] = Base64.decodeBase64(crntImage);
          FileOutputStream fos = new FileOutputStream(new File("c:/decode/abc.bmp")); 
          fos.write(dearr); 
          fos.close();
      

      【讨论】:

        【解决方案4】:
        import java.util.Base64;
        

        ....只是明确表示此答案使用 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...");
        }
        

        【讨论】:

          【解决方案5】:

          使用 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 );
          

          【讨论】:

            【解决方案6】:

            试试这个:

            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");
                }
            }
            

            【讨论】:

            • OP 没有 URL。他们有 base64 中的图像数据-您的答案完全忽略了这一点。
            猜你喜欢
            • 2014-08-01
            • 1970-01-01
            • 2016-01-13
            • 1970-01-01
            • 2013-12-14
            • 1970-01-01
            • 2010-12-04
            • 2013-01-31
            • 2011-04-12
            相关资源
            最近更新 更多