How can I convert the java Object into a InputStream?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use ObjectOutputStream
You write the object (obj in the code below) to the ObjectOutputStream, your object you want to convert to an input stream must implement Serializable.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
oos.close();
InputStream is = new ByteArrayInputStream(baos.toByteArray());