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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2764504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:36:01  来源:igfitidea点击:

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

javabase64mimejavamail

提问by Sebastiaan van den Broek

I am trying to get the base64 content of a MimePart in a MimeMultiPart, but I'm struggling with the Javamail package. 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. 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)

我正在尝试在 MimeMultiPart 中获取 MimePart 的 base64 内容,但我正在努力使用 Javamail 包。我只是想要某个内联图像的 base64 编码字符串,但似乎没有一种简单的方法可以做到这一点。我写了一个方法,将 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: [email protected]

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: <[email protected]>
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 字符串的帮助。

采纳答案by Sebastiaan van den Broek

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.

现在它显示图像就好了。