将EmailMessage转换为ByteArrayOutputStream [英] Converting EmailMessage to ByteArrayOutputStream
问题描述
我正在使用Java EWS API(EWS-Exchange Web服务)从Exchange服务器获取电子邮件,并将其存储在专有CMS中.我收到消息的类型是 microsoft.exchange.webservices.data.EmailMessage
-EWS API提供的类.CMS API需要 ByteArrayOutputStream
对象作为其方法的参数.
I am fetching email from Exchange server using Java EWS API (EWS - Exchange Web Services) and storing it in a proprietary CMS. The type in which I am getting message is microsoft.exchange.webservices.data.EmailMessage
- a class provided by EWS API. The CMS API requires ByteArrayOutputStream
object as a parameter to its method.
所以我想将 EmailMessage
对象转换为 ByteArrayOutputStream
.我看到了此线程,并尝试了类似的操作:( item
下面的类型为 EmailMessage
)
So I want to convert EmailMessage
object to ByteArrayOutputStream
. I saw this thread and tried similar like this: (Below item
is of type EmailMessage
)
ByteArrayOutputStream b = new ByteArrayOutputStream();
try
{
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject((Object)item);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
但是我得到
java.io.NotSerializableException: microsoft.exchange.webservices.data.EmailMessage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
我能够使用 FileOutputStream
将这些 EmailMessage
对象保存为 .eml
格式,但是现在我无法找到解决方法将它们转换为 ByteArrayOutputStream
.因此,有什么方法可以将 FileOutputStream
转换为 ByteArrayOutputStream
,或者直接将其从 EmailMessage
转换为 ByteArrayOutputStream
.
I am able to save these EmailMessage
objects in .eml
format using FileOutputStream
, however now I am not able to find the way to convert them to ByteArrayOutputStream
.
So is there any way to convert FileOutputStream
to ByteArrayOutputStream
or just directly from EmailMessage
to ByteArrayOutputStream
.
推荐答案
您会遇到此异常,因为您的(Object)item
的类未实现 Serializable
接口.从Java文档
You are getting this exception because your (Object)item
's class not implementing Serializable
interface. From java doc writeObject(Object obj)
对于OutputStream的问题和不应序列化的类,将引发异常.所有异常对于OutputStream都是致命的,OutputStream处于不确定状态,并且取决于调用者忽略还是恢复流状态.
Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, which is left in an indeterminate state, and it is up to the caller to ignore or recover the stream state.
这篇关于将EmailMessage转换为ByteArrayOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!