Getting base64 content string of image from mimepart in Java

I am trying to get base64 content of MimePart into MimeMultiPart but I am struggling with Javamail package. I just want a base64 encoded string of a specific inline image, however, it looks like this is an easy way to do it. I wrote a method that will use the mime content (as a string) and the image name as a parameter, and looks for the part containing the base64 content of that image name, and at the end it returns that base64 string (as well as the content type, but it doesn't matter for this question)

Here's the relevant code (including the 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 insert all the output as the message will be too long, but these are some of them:

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

      

This is the mimeString input part, it finds this (correct) image name part:

--_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 out the encoded mime string, this is the other string I was expecting:

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

      

Obviously different from the one that has its conclusion in the part above. I'm not even sure what I am seeing here, but when I try to load this as an image in an html page, it doesn't work.

This is pretty frustrating for me as all I want is a piece of text I'm already typing, but I wouldn't have to search for the mime string myself for the correct part, representing all kinds of bugs.So I'd rather use the Javamail library, but could would use some help on how to actually get the correct mime string.

+2


a source to share


1 answer


Eliminated my problem, modified code:

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 displays the image just fine.

+6


a source







All Articles