Search code examples
javahttprequestobjectinputstream

ObjectInputStream gives StreamCorruptedException


I want to convert the inputStream (HttpRequest.getInputStream()) to an Object.

try {
        ObjectInput ois = new ObjectInputStream (inputStream);
        resObject= ois.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

the inputStream contains xml in the form of String.


Solution

  • Cannot directly use ObjectInputStream here because, it expects a serialized object, not an XML string.

    What you probably could do is unmarshal the inputstream content into the desired object using an unmarshaller / Java XML binding APIs. like JAXB and quite a few other APIs out there.

    A sample link to get you started on this : Tutorials point link And unmarshalling in general : java.net link which has a little explanation as well.

    Hope this helps.