从Java中的mimepart获得的图像的base64内容字符串 [英] getting base64 content string of an image from a mimepart in Java

查看:255
本文介绍了从Java中的mimepart获得的图像的base64内容字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个MimeMultiPart类一MimePart的内容​​的base64,但我与JavaMail包挣扎。我只是想为Base64恩一定内嵌图像的codeD字符串,似乎没有成为一个简单的方法,虽然做到这一点。
我写的,将采取MIME内容(作为一个字符串)包含映像名称为Base64内容的一部分的方法和图像名作为参数,并搜索,并在最后返回这个的base64字符串(以及内容类型,但是这是无关这个问题)

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)

下面是相关code(包括相关进口):

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

这是mimeString输入的一部分,但它确实找到映像名称此(正确)的部分:

This is a part of the mimeString input, it does find this (correct) part with the image name:

--_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)

但是,当它终于打印连接codeD MIME字符串,这是一个不同的字符串比我期待的:

But when it finally prints the encoded mime string, this is a different string than I was expecting:

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

这具有其在上面的部分输出所述一个显然不同。我甚至不知道我在寻找什么在这里,但是当我尝试在html网页加载这个作为一个图像,它不会工作。

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.

这对我来说是令人沮丧的公平,因为所有我要的是一个一块我已经打印文本,但我宁愿没有通过哑剧字符串自己搜索正确的部分,引入各类bugs.So我真的preFER使用JavaMail库,但可以使用上如何真正得到正确的MIME字符串一些帮助。

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.

推荐答案

解决我的问题,修改code为:

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.

这篇关于从Java中的mimepart获得的图像的base64内容字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆