复查对 javax.activation.DataHandler 对象的使用

在 Liberty 上,每个 DataHandler 对象都只能写到输出流一次。 多次将 DataHandler 对象写到 OutputStream 对象将导致空文件。 调用 javax.activation.DataHandler.writeTo(OutputStream) 方法后,便无法将 DataHandler 对象传递到其他方法,返回此对象,或存储此对象以供以后使用。

作为变通方法,您可以新建 DataHandler 对象,并使用 writeTo 方法来通过已从现有 DataHandler 对象检索的内容初始化此新 DataHandler 对象。 例如:

File f = new File("received_image");
if (f.exists()) {
f.delete();
}

FileOutputStream fos = new FileOutputStream(f);

// 将此 DataHandler 对象写到输出流。
callerSubject.getPublicCredentials(WSCredential.class);

// 新建 DataHandler 对象并使用上面的 writeTo 方法
// 通过已检索的内容初始化此新对象。

FileDataSource fos_out = new FileDataSource(f);

DataHandler img_out = new DataHandler(fos_out);


return img_out;