我正在使用 Java EWS API(EWS - Exchange Web 服务)从 Exchange 服务器获取电子邮件并将其存储在专有 CMS 中。我收到消息的类型是 microsoft.exchange.webservices.data.EmailMessage
- EWS API 提供的类。 CMS API 需要 ByteArrayOutputStream
对象作为其方法的参数。
所以我想将 EmailMessage
对象转换为 ByteArrayOutputStream
。我看到了this thread并尝试类似这样:(item
下方是 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
以 .eml
格式保存这些 EmailMessage
对象,但是现在我找不到方法将它们转换为 ByteArrayOutputStream
。
那么有什么方法可以将 FileOutputStream
转换为 ByteArrayOutputStream
或者直接从 EmailMessage
转换为 ByteArrayOutputStream
。
最佳答案
您收到此异常是因为您的(Object)item
类未实现Serializable
接口(interface)。来自 java 文档 writeObject(Object obj)
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.
关于java - 将 EmailMessage 转换为 ByteArrayOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19704462/