有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图在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: image001.gif@01CAD280.4D637150

这是模拟字符串输入的一部分,它确实找到了具有图像名称的(正确)部分:

--_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: <image001.gif@01CAD280.4D637150>
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字符串方面使用一些帮助


共 (1) 个答案

  1. # 1 楼答案

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

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

    现在它可以很好地显示图像了