简体   繁体   English

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

[英]getting base64 content string of an image from a mimepart in Java

I am trying to get the base64 content of a MimePart in a MimeMultiPart, but I'm struggling with the Javamail package. 我试图在MimeMultiPart中获取MimePart的base64内容,但我正在努力使用Javamail包。 I simply want the base64 encoded String of a certain inline image, there doesn't seem to be an easy way to do this though. 我只想要某个内嵌图像的base64编码字符串,但似乎没有一种简单的方法可以做到这一点。 I wrote a method that will take the mime content (as a string) and an image name as a parameter, and searches for the part that contains the base64 content of that image name, and in the end returns this base64 string (as well as the content type but that is irrelevant for this question) 我写了一个方法,将mime内容(作为字符串)和图像名称作为参数,并搜索包含该图像名称的base64内容的部分,并在最后返回此base64字符串(以及内容类型但与此问题无关)

Here is the relevant code (including relevant imports): 以下是相关代码(包括相关导入):

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

I cannot paste all of the output as the post would be too long, but this is some of it: 我无法粘贴所有输出,因为帖子太长了,但这是其中的一部分:

image name: image001.gif@01CAD280.4D637150

This is a part of the mimeString input, it does find this (correct) part with the image name: 这是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: <image001.gif@01CAD280.4D637150>
Content-Transfer-Encoding: base64

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

But when it finally prints the encoded mime string, this is a different string than I was expecting: 但是当它最终打印出编码的mime字符串时,这是一个不同于我期望的字符串:

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

Clearly different from the one that has its output in the part above. 明显不同于上面部分中有输出的那个。 I'm not even sure what I'm looking at here, but when I try to load this as an image in a html page, it won't work. 我甚至不确定我在这里看到的是什么,但当我尝试将其作为html页面中的图像加载时,它将无法正常工作。

This is fairly frustrating for me, since all I want is a piece of the text that I'm already printing, but I'd rather not have to search through the mime string myself for the correct part, introducing all kinds of bugs.So I'd really prefer to use the Javamail library but could use some help on how to actually get that correct mime string. 这对我来说是相当令人沮丧的,因为我想要的只是我已经打印的文本的一部分,但我宁愿不必自己搜索mime字符串以找到正确的部分,引入各种错误。所以我真的更喜欢使用Javamail库,但可以使用一些帮助来实际获取正确的mime字符串。

Solved my issue, modified code to: 解决了我的问题,修改了代码:

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

And now it's displaying the image just fine. 而现在它正好显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM