我想獲取MimeMultiPart中的MimePart的base64內容,但是我正在努力處理Javamail包。我只是想要一個特定的內聯圖像的base64編碼的字符串,似乎並沒有一個簡單的方法來做到這一點。 我寫了一個將mime內容(作爲字符串)和圖像名稱作爲參數的方法,並搜索包含該圖像名稱的base64內容的部分,並在最後返回此base64字符串(以及作爲內容類型,但是這是不相關的這個問題)從Java中的mimepart獲取圖像的base64內容字符串
下面是相關代碼(包括相關進口):
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;
}
我不能粘貼所有的輸出作爲後會過長,但這是其中的一部分:
image name: [email protected]
這是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)
但是,當它終於打印編碼的MIME字符串,這是一個不同的字符串比我期待的:
encoded mime string: R0lGODlhEAAQAO+/vQAAHG4c77+90pQ877+9HGzvv71cdO+/vWzvv73vv71077+977+977+9XO+/vUx077+9XO+/vcqE77+92qwk77+9HEzvv70kfO+/vWzvv73alO+
與上面部分的輸出結果明顯不同。我甚至不確定我在這裏看到什麼,但是當我嘗試在HTML頁面中將其作爲圖像加載時,它不起作用。
這對我來說相當令人沮喪,因爲我想要的只是我已經打印的一段文本,但我寧願不必通過MIME字符串搜索正確的部分,引入各種所以我真的更喜歡使用Javamail庫,但可以使用一些關於如何獲得正確的MIME字符串的幫助。
爲了避免意外/混淆,我會避免使用方法名作爲變量名,就像您使用encodeBase64一樣。 – bluish 2011-07-13 13:06:06
是的,你是對的,不知道這是如何滑入。可能需要鍵入encodedBase64。 – 2011-07-14 13:27:33