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

22,561

Solved my issue, modified code to:

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

And now it's displaying the image just fine.

Share:
22,561
Sebastiaan van den Broek
Author by

Sebastiaan van den Broek

Updated on July 05, 2022

Comments

  • Sebastiaan van den Broek
    Sebastiaan van den Broek almost 2 years

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

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