2013-01-20 48 views
1

我試圖用Jython 2.5.3獲取附加到電子郵件的圖像。我收到了電子郵件(使用他們的Python imap庫的Jython版本)。我可以通過部分循環,使用get_content_type()找到正確的零件類型得到的附接:如何使用Jython保存電子郵件圖像附件

image, img_ext = None, None 
for part in self.mail.get_payload(): 
    part_type, part_ext = part.get_content_type().split('/') 
    part_type = part_type.lower().strip() 
    part_ext = part_ext.lower().strip() 
    if part_type == 'image': 
     image = part.get_payload(decode=True) 
     img_ext = part_ext 
return image, img_ext 

「圖像」被返回作爲一個字節大的塊,這在常規的Python我直接寫出到一個文件。然而,當我嘗試在Jython中同樣的事情,我得到以下錯誤:

TypeError: write(): 1st arg can't be coerced to java.nio.ByteBuffer[], java.nio.ByteBuffer 

什麼是讓Jython的認識我的數據的大斑點的字節數組的正確方法?

PS:寫代碼使用tempfile.mkstmp(),默認爲寫入二進制...

+0

請更新問題,而不是通過評論。 – rae1

回答

0

對於未來的讀者,這裏就是我得到了周圍。在代碼中寫的是:

from org.python.core.util import StringUtil 
from java.nio import ByteBuffer 


tmp, filename = tempfile.mkstemp(suffix = "." + extension, text=True) 
bytes = StringUtil().toBytes(attachment) 
bb = ByteBuffer.wrap(bytes) 
tmp.write(bb) 
tmp.close() 
相關問題