当从ObjectInputStream中读取对象时,出现EOFException?

问题描述 投票:-1回答:1

我正在插入一个从xml解压缩中获得的可序列化对象,在插入对象时,我使用了以下方法,即insertPOJOInBackend()。为了插入对象,我使用了下面的方法,即insertPOJOInBackend()。然而,当使用readPOJOFromBackend()方法检索对象时,我得到了一个EOFException。在过去的一周里,我一直在与这个异常斗争。如果能给我一点帮助,我将非常感激。

insertPOJOInBackend(String key, String url, Serializable Object){

   //obtained connection object

    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new 
    BufferedOutputStream(connection.getOutputStream()));

    objectOutputStream.writeObject(serializableObject);
    objectOutputStream.close();

    BufferedReader rd = null;
    StringBuilder sb = null;
    String line = null;

    rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    sb = new StringBuilder();
    while ((line = rd.readLine()) != null) {
      sb.append(line);
    }

}

readPOJOFromBackend() {

HttpsURLConnection connection = null;
    try
    {
      //obtained connection object

      ObjectInputStream objectInputStream = new ObjectInputStream(new 
      BufferedInputStream(connection.getInputStream()));

      connection.getInputStream().close();

      responseObject = objectInputStream.readObject();

      objectInputStream.close();
    }
    catch (EOFException eof)
    {
      LOG.error("EOFException");
    }
}

我是否太早关闭ObjectInputStream的输入流,或者请检查我哪里出错了?如果你需要任何其他信息,请让我知道,我在调用objectInputStream.readObject时得到了EFOFException。PFB的堆栈跟踪。

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2669)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3413)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:3214)
    at java.io.ObjectInputStream.readString(ObjectInputStream.java:1896)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1558)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2276)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2200)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:426)
    at java.util.ArrayList.readObject(ArrayList.java:778)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1159)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2167)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2276)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2200)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:426)
    at java.util.ArrayList.readObject(ArrayList.java:778)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1159)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2167)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2276)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2200)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2276)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2200)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2276)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2200)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2058)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:426)
    at **.**.**.common.backend.impl.BackednImpl.readPOJOFromBackend(XXXXXImpl.java:812)
java serialization objectinputstream eofexception
1个回答
0
投票

非常感谢@Amongalen的解决和快速响应。

我把connection.getInputStream().close()移到了调用objectInputStream.readObject()之后,它就像预期的那样工作了。

Phew. 整整一个星期!!!

© www.soinside.com 2019 - 2024. All rights reserved.