简体   繁体   English

构造ObjectInputStream时出现EOFException

[英]EOFException while constructing an ObjectInputStream

i'm trying to serialize a HashMap on serverside(Tomcat 6.0.29) and to deserealize it on a client-side android application. 我正在尝试在服务器端(Tomcat 6.0.29)上序列化HashMap,并在客户端android应用程序上将其散化。

First time i've tried this i got an StreamCorruptedException, but after i created a completly new servlet with a clean doGet/doPost-Method i've managed to solve this issue, unfortunatly i get an EOFException instead. 第一次尝试此操作时,我遇到了StreamCorruptedException,但是在用干净的doGet / doPost-Method创建了一个全新的servlet之后,我设法解决了这个问题,但是不幸的是,我得到了EOFException。

So first i'm posting my code and then i'll explain my problem in detail: 因此,我首先发布代码,然后详细解释我的问题:

Server-Side Code (this will be executed when doPost Or doGet is invoked): 服务器端代码(将在调用doPost或doGet时执行):

    if ("object".equals(request.getParameter("format")))
        {

            trace.log("Before Serializing");
            ObjectOutputStream out = null;
            try
            {
                out = new ObjectOutputStream(response.getOutputStream());
                out.writeObject(outerTable);
                trace.log("after Serializing");
            }
            finally
            {
                if(out != null)
                {
                    try
                    {
                        out.flush();
                        out.close();
                    }
                    catch(Exception ex)
                    {
                        trace.log("Exception has been thrown "+ex.getMessage());
                    }
                }
            }
            trace.log("after closing stream");
        }
        else
        {
            PrintWriter out = response.getWriter();
            out.println(outerTable.toString());
        }

outerTable is a HashMap wich is created exactly before this code-block and it has always correct values. outsideTable是在此代码块之前恰好创建的HashMap,它始终具有正确的值。

There are no errors thrown on server-side. 在服务器端没有抛出任何错误。 When i try to access the servlet in my webbrowser with parameter "?format=object" i get the whole serialization string. 当我尝试使用参数“?format = object”访问Web浏览器中的servlet时,我得到了整个序列化字符串。

Script of Android-Application (this Method executes the http request after a specific button is pressed): Android应用程序的脚本(此方法在按下特定按钮后执行http请求):

    private synchronized void performNetworkAccess()
    {
  Runnable run = new Runnable()
  {
    @Override
    public void run()
    {
    System.out.println("in run()-Method");
    ObjectInputStream objInput = null;
    try
    {
        URL url = new URL(myWebsite);
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream(); 
        objInput = new ObjectInputStream(in);

        outerTable = (HashMap<String, HashMap<String, Object>>)              objInput.readObject();
        synchronized (monitor)
        {
        monitor.notifyAll();
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        if (objInput != null)
        {
        try
        {
            objInput.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        }
    }

    }

};
Thread t1 = new Thread(run);
t1.start();
}

The Exception occurs immediatly after the constructor of ObjectInputStream class is invoked. 调用ObjectInputStream类的构造函数后,立即发生异常。

Stacktrace: 堆栈跟踪:

  • W/System.err(1797): java.io.EOFException W / System.err(1797):java.io.EOFException
  • W/System.err(1797): at libcore.io.Streams.readFully(Streams.java:83) W / System.err(1797):位于libcore.io.Streams.readFully(Streams.java:83)
  • W/System.err(1797): at java.io.DataInputStream.readShort(DataInputStream.java:169) W / System.err(1797):位于java.io.DataInputStream.readShort(DataInputStream.java:169)
  • W/System.err(1797): at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2102) W / System.err(1797):位于java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2102)
  • W/System.err(1797): at java.io.ObjectInputStream.(ObjectInputStream.java:372) W / System.err(1797):位于java.io.ObjectInputStream。(ObjectInputStream.java:372)
  • W/System.err(1797): at de.tesla.jtheseuscontactsync.ContactSync$4.run(ContactSync.java:602) W / System.err(1797):at de.tesla.jtheseuscontactsync.ContactSync $ 4.run(ContactSync.java:602)
  • W/System.err(1797): at java.lang.Thread.run(Thread.java:856) W / System.err(1797):位于java.lang.Thread.run(Thread.java:856)

I've already found many occurences of this problem, but i didn't see one of them where this error occures before readObject() is invoked. 我已经发现许多此问题的发生,但是在调用readObject()之前,我没有看到其中发生此错误的其中之一。

Things i've already tried: 我已经尝试过的事情:

  • add out.flush() shortly after constructing ObjectOutputStream 在构造ObjectOutputStream之后不久添加out.flush()
  • remove out.flush() and/or out.close() method(s) 删除out.flush()和/或out.close()方法
  • wrap Input-/OutputStream with BufferedInput-/-OutputStream 用BufferedInput-/-OutputStream包装Input- / OutputStream
  • retrieve stream as a file and try to deserealize the file 将流作为文件检索,并尝试将文件退化

The odd thing is when i first write the Object to a file via FileOutputStream and access this file (on the server) from the client via Url.openStream() then ObjectOutputStream can read this object perfectly, unfortunatly this would mean a lot of trouble when multiple users trying to retrieve this object at the same time... 奇怪的是,当我首先通过FileOutputStream将对象写入文件并通过Url.openStream()从客户端访问该文件(在服务器上)时,ObjectOutputStream可以完美地读取该对象,不幸的是,这将在您遇到麻烦时多个用户试图同时检索此对象...

i hope you can help me out with this one :) 我希望你能帮我这个忙:)

The servlet has got an exception of some kind and hasn't even constructed the ObjectOutputStream. servlet有某种例外,甚至还没有构造ObjectOutputStream。 You need to check the response code for 200 before you try to construct the ObjectInputStream at the client. 在尝试在客户端上构造ObjectInputStream之前,需要检查200的响应代码。

Some crazy idea... try to add a Content-Length header to your response. 一些疯狂的主意...尝试在您的响应中添加Content-Length标头。 Could imply writing your bytes to a ByteArrayOutputStream, measuring the size, setting the header and only then writing to response... but could be the reason that URLConnection is prematurely cutting the connection... 可能暗示将字节写入ByteArrayOutputStream,测量大小,设置标头然后仅写入响应...但是这可能是URLConnection过早切断连接的原因...

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObejctOutputStream oos = new ObejctOutputStream(baos);
oos.write(...);

byte[] data = baos.toByteArray();

response.setHeader("Content-Length", "" + data.length);
response.getOutputStream().write(data);

Java Serialization Java序列化

@Robert comment is right. @Robert评论是正确的。 Java serialization is not intented for communication between different environments. Java序列化不适合在不同环境之间进行通信。 More if you're working between JVM and Dalvik. 如果您在JVM和Dalvik之间工作,则需要更多信息。 So another framework would be more suitable (something with XML or JSON could be nice, even wrapping it with a GZip[]Stream). 因此,另一个框架会更合适(使用XML或JSON的东西可能会更好,甚至可以使用GZip [] Stream将其包装)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM