【问题标题】:java mail Base64 encoded string to image attachmentjava邮件Base64编码字符串到图像附件
【发布时间】:2015-05-12 22:53:20
【问题描述】:

我有一个使用 JSON 发布到 Spring 表单中的 base64 编码字符串。

data:image/png;base64,iVBORw0KGg......etc

我想将此图像添加为电子邮件的附件。附加文件工作正常,但它只是添加 base64 字符串作为附件。

我正在使用以下代码来创建附件部分。

private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {

    if (fileName == null || fileContent == null) {
        return null;
    }

    LOG.debug("addAttachment()");

    MimeBodyPart filePart = new MimeBodyPart();

    String data = fileContent;
    DataSource ds;
    ds = new ByteArrayDataSource(data.getBytes(), "image/*");

    // "image/*"
    filePart.setDataHandler(new DataHandler(ds));
    filePart.setFileName(fileName);

    LOG.debug("addAttachment success !");

    return filePart;

}

我也试过了

 ds = new ByteArrayDataSource(data, "image/*");

如何使用 ByteArrayDataSource 将 base64 字符串转换为正确的图像文件?

【问题讨论】:

    标签: java jakarta-mail


    【解决方案1】:

    为避免解码和重新编码,您可以使用 javax.mail.internet.PreencodedMimeBodyPart 加载您的 base64 字符串并将 PreencodedMimeBodyPart 附加到您的消息中。

        private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {
            if (fileName == null || fileContent == null) {
                return null;
            }
    
            LOG.debug("addAttachment()");
            MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
            filePart.setContent(fileContent, "image/*");
            LOG.debug("addAttachment success !");
            return filePart;
        }
    

    否则,您可以使用 javax.mail.internet.MimeUtility::decode 包装与数据源一起使用的输入流,但这将对给定数据进行解码和重新编码。

        private MimeBodyPart addAttachment(final String fileName, final String fileContent) throws MessagingException {
            if (fileName == null || fileContent == null) {
                return null;
            }
    
            LOG.debug("addAttachment()");
            MimeBodyPart filePart = new MimeBodyPart();
    
            String data = fileContent;
            DataSource ds;  //Assuming fileContent was encoded as UTF-8.
            InputStream in = new ByteArrayInputStream(data.getBytes("UTF-8"));
            try {
                in = MimeUtility.decode(in, "base64");
                try {
                    ds = new ByteArrayDataSource(in , "image/*");
                } finally {
                    in.close();
                }
            } catch (IOException ioe) {
                throw new MessagingException(fileName, ioe);
            }
    
            // "image/*"
            filePart.setDataHandler(new DataHandler(ds));
            filePart.setFileName(fileName);
    
            LOG.debug("addAttachment success !");
            return filePart;
        }
    

    【讨论】:

      【解决方案2】:

      您必须首先使用 Base64 解码器。使用 Java 8,您可以:

      byte[] imgBytes = Base64.getDecoder().decode(base64String);
      

      对于较旧的 java 版本,您将不得不使用一些库,如 apache commons-codec 之类的 - 周围有很多。

      【讨论】:

      • 酷,我会试试这个:byte[] blah = Base64.decodeBase64(data.getBytes());
      • 嗨科恩..它对你有用吗?我试过这个,但我的电子邮件中有一张空白图片。
      【解决方案3】:

      我遇到了同样的问题,我可以使用以下代码修复它:

      //您必须先解析数据以删除“data:image/png;base64”,然后才能使用decodeBase64(data)。

      byte[] imgBytes = Base64.decodeBase64(data);
      ByteArrayDataSource dSource = new ByteArrayDataSource(imgBytes, "image/*");
      

      【讨论】:

        【解决方案4】:
         // v = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA........................
        String body = v.replace("data:image/png;base64","");                                    
        MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
        filePart.setFileName("screenshot.png");
        //This is Needed if you want to show as an html element in the page
        filePart.setHeader("Content-ID", "<screenshot>");
        filePart.setText(body);
        message.getMimeMultipart().addBodyPart(filePart);
        

        【讨论】:

        • 欢迎来到 Stack Overflow!请尝试提供有关您的解决方案如何工作的精彩描述。请参阅:How do I write a good answer? 谢谢
        【解决方案5】:

        在@jmehrens 的回答下,我可以附加一个文件,但无法打开它们。原来您需要单独设置数据类型并将其从给定的文件内容字符串中删除:

        {
           String dataType = StringUtils.substringBetween(file, "data:", ";base64,"); // extract data type (dataType = "image/png") 
        base64EncodedFileContent = fileContent.replaceFirst("data:.*;base64,", ""); // remove prefix from fileContent String (base64EncodedFileContent = "iVBORw0KGg......etc"
        
            MimeBodyPart filePart = new PreencodedMimeBodyPart("base64");
            filePart.setContent(base64EncodedFileContent, dataType);
            filePart.setFileName(fileName);
        
            return filePart;
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-07
          • 2023-03-07
          相关资源
          最近更新 更多