从Java中的mimepart获取图像的base64内容字符串

2024-02-21

我正在尝试获取 MimeMultiPart 中 MimePart 的 base64 内容,但我在使用 Javamail 包时遇到了困难。我只是想要某个内联图像的 base64 编码字符串,但似乎没有一种简单的方法可以做到这一点。 我写了一个方法,它将mime内容(作为字符串)和图像名称作为参数,并搜索包含该图像名称的base64内容的部分,最后返回这个base64字符串(以及内容类型,但这与这个问题无关)

这是相关代码(包括相关导入):

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import com.sun.mail.util.BASE64DecoderStream;



private static String[] getBase64Content(String imageName, String mimeString) throws MessagingException, IOException
 {
  System.out.println("image name: " + imageName + "\n\n");
  System.out.println("mime string: " + mimeString);

  String[] base64Content = new String[2];
  base64Content[0] = "";
  base64Content[1] = "image/jpeg"; //some default value

  DataSource source = new ByteArrayDataSource(new ByteArrayInputStream(mimeString.getBytes()), "multipart/mixed");  
  MimeMultipart mp = new MimeMultipart(source);

  for (int i = 0; i < mp.getCount(); i++)
  {
   MimePart part = (MimePart) mp.getBodyPart(i);
   String disposition = part.getDisposition();
   if (disposition != null && disposition.equals(Part.INLINE))  
   {
    if (part.getContentID() != null && part.getContentID().indexOf(imageName) > -1) //check if this is the right part
    {
     if (part.getContent() instanceof BASE64DecoderStream)
     {
      BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) part.getContent();
      StringWriter writer = new StringWriter();
      IOUtils.copy(base64DecoderStream, writer);
      String base64decodedString = writer.toString();
      byte[] encodedMimeByteArray = Base64.encodeBase64(base64decodedString.getBytes());
      String encodedMimeString = new String(encodedMimeByteArray);
      System.out.println("encoded mime string: " + encodedMimeString);
      base64Content[0] = encodedMimeString;
      base64Content[1] = getContentTypeString(part);
     } 
    }
   }
  }
  return base64Content; 
 }

我无法粘贴所有输出,因为帖子太长,但这是其中的一些:

image name: [email protected] /cdn-cgi/l/email-protection

这是 mimeString 输入的一部分,它确实找到了带有图像名称的(正确的)部分:

--_004_225726A14AF9134CB538EE7BD44373A04D9E3F3940menexch2007ex_
Content-Type: image/gif; name="image001.gif"
Content-Description: image001.gif
Content-Disposition: inline; filename="image001.gif"; size=1070;
 creation-date="Fri, 02 Apr 2010 16:19:43 GMT";
 modification-date="Fri, 02 Apr 2010 16:19:43 GMT"
Content-ID: <[email protected] /cdn-cgi/l/email-protection>
Content-Transfer-Encoding: base64

R0lGODlhEAAQAPcAABxuHJzSlDymHGy2XHTKbITCdNTu1FyqTHTCXJTKhLTarCSKHEy2JHy6bJza
lITKfFzCPEyWPHS+XHzCbJzSjFS+NLTirBx6HHzKdOz27GzCZJTOjCyWHKzWpHy2ZJTGhHS+VLzi
(more base64 string here that I'm not going to paste)

但是当它最终打印编码的 mime 字符串时,这是一个与我预期不同的字符串:

encoded mime string: R0lGODlhEAAQAO+/vQAAHG4c77+90pQ877+9HGzvv71cdO+/vWzvv73vv71077+977+977+9XO+/vUx077+9XO+/vcqE77+92qwk77+9HEzvv70kfO+/vWzvv73alO+

与上面部分中的输出明显不同。我什至不确定我在这里看到的是什么,但是当我尝试将其作为图像加载到 html 页面中时,它不起作用。

这对我来说相当令人沮丧,因为我想要的只是我已经打印的一段文本,但我不想自己在 mime 字符串中搜索正确的部分,从而引入各种错误。我真的更喜欢使用 Javamail 库,但可以使用一些有关如何实际获取正确的 mime 字符串的帮助。


解决了我的问题,将代码修改为:

if (part.getContent() instanceof BASE64DecoderStream)
{
    BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) part.getContent();
    byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
    byte[] encodedBase64 = Base64.encodeBase64(byteArray);
    base64Content[0] = new String(encodedBase64, "UTF-8");
    base64Content[1] = getContentTypeString(part);
}

现在它可以正常显示图像了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从Java中的mimepart获取图像的base64内容字符串 的相关文章

随机推荐