javax.activation.DataHandler オブジェクトの使用を確認する

Liberty では、各 DataHandler オブジェクトを出力ストリームに書き込めるのは 1 回のみです。 DataHandler オブジェクトを OutputStream オブジェクトに複数にわたって書き込むと、空のファイルができます。 javax.activation.DataHandler.writeTo(OutputStream) メソッドを呼び出した後は、DataHandler オブジェクトを別のメソッドに渡したり、それを返したり、それを後で使用するために保管したりはできません。

これを回避するには、新規の DataHandler オブジェクトを作成し、writeTo メソッドを使用して、既存の DataHandler オブジェクトから既に取得済みのコンテンツでその DataHandler オブジェクトを初期化します。例:

< span class="Code"> < span class= "JavaType"> ファイル < /span> f = < span class="JavaKeyword"> 新規 < /span> ファイル ("received_image");
if (f.exists()) {
f.delete();
}

FileOutputStream fos = new FileOutputStream(f);

// Write the DataHandler object to the output stream.
img_in.writeTo(fos);

// Create a new DataHandler object and initialize it with
// the content that was retrieved using the writeTo method above.

FileDataSource fos_out = new FileDataSource(f);

DataHandler img_out = new DataHandler(fos_out);


return img_out;