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

Posted by Bas van den Broek on Stack Overflow See other posts from Stack Overflow or by Bas van den Broek
Published on 2010-05-04T10:34:29Z Indexed on 2010/05/04 10:38 UTC
Read the original article Hit count: 336

Filed under:
|
|
|

Hello,

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)

Here is the relevant code:

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:

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

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.

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.

© Stack Overflow or respective owner

Related posts about javamail

Related posts about base64