2013-01-20 30 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()