How can I convert an Object to Inputstream

2019-02-04 04:00发布

How can I convert the java Object into a InputStream?

1条回答
孤傲高冷的网名
2楼-- · 2019-02-04 04:34

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());
查看更多
登录 后发表回答