构造ObjectInputStream时出现EOFException [英] EOFException while constructing an ObjectInputStream

查看:498
本文介绍了构造ObjectInputStream时出现EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器端(Tomcat 6.0.29)上序列化HashMap并在客户端的Android应用程序上对其进行deserealize。

i'm trying to serialize a HashMap on serverside(Tomcat 6.0.29) and to deserealize it on a client-side android application.

我第一次我试过这个我得到了一个StreamCorruptedException,但是在我创建了一个带有干净的doGet / doPost-Method的完全新的servlet后,我设法解决了这个问题,不幸的是我得到了一个EOFException。

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.

首先我发布我的代码,然后我会详细解释我的问题:

So first i'm posting my code and then i'll explain my problem in detail:

服务器-Side Code(这将在调用doPost或doGet时执行):

Server-Side Code (this will be executed when doPost Or doGet is invoked):

    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是在此代码块之前创建的HashMap,它具有总是正确的值。

outerTable is a HashMap wich is created exactly before this code-block and it has always correct values.

服务器端没有抛出任何错误。
当我尝试使用参数?format = object访问我的webbrowser中的servlet时,我得到
整个序列化字符串。

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.

脚本Android-Application(此方法在按下特定按钮后执行http请求):

Script of Android-Application (this Method executes the http request after a specific button is pressed):

    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();
}

异常在调用ObjectInputStream类的构造函数后发生。

The Exception occurs immediatly after the constructor of ObjectInputStream class is invoked.

Stacktrace:

Stacktrace:


  • W / System.err(1797):java.io.EOFException

  • W / System.err(1797):at libcore.io.Streams.readFully(Streams.java:83)

  • W / System。 err(1797):at java.io.DataInputStream.readShort(DataInputStream.java:169)

  • W / System.err(1797):at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream) .java:2102)

  • W / System.err(1797):at 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 java。 lang.Thread.run(Thread.java:856)

  • W/System.err(1797): java.io.EOFException
  • W/System.err(1797): at libcore.io.Streams.readFully(Streams.java:83)
  • W/System.err(1797): at java.io.DataInputStream.readShort(DataInputStream.java:169)
  • W/System.err(1797): at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2102)
  • W/System.err(1797): at 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 java.lang.Thread.run(Thread.java:856)

我已经发现很多这个问题,但我没有'看到其中一个在调用readObject()之前发生此错误。

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.

Th我已经尝试过:


  • 在构建ObjectOutputStream后不久添加out.flush()

  • 删除out.flush()和/或out.close()方法

  • 使用BufferedInput包装Input- / OutputStream - / - OutputStream

  • 将流检索为文件并尝试去实现文件

  • add out.flush() shortly after constructing ObjectOutputStream
  • remove out.flush() and/or out.close() method(s)
  • wrap Input-/OutputStream with BufferedInput-/-OutputStream
  • retrieve stream as a file and try to deserealize the file

奇怪的是当我第一次通过FileOutputStream将对象写入文件时并通过Url.openStream()从客户端访问此文件(在服务器上),然后ObjectOutputStream可以完美地读取此对象,不幸的是,当多个用户同时尝试检索此对象时,这将意味着很多麻烦...

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...

我希望你能帮助我解决这个问题:)

i hope you can help me out with this one :)

推荐答案

一些疯狂的想法...尝试在您的回复中添加 Content-Length 标头。可能意味着将你的字节写入ByteArrayOutputStream,测量大小,设置标题然后只写入响应...但可能是URLConnection过早切断连接的原因......

Some crazy idea... try to add a Content-Length header to your response. 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 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序列化

@Robert评论是对的。 Java序列化不是用于不同环境之间的通信。如果您在JVM和Dalvik之间工作,那就更多了。所以另一个框架会更合适(使用XML或JSON的东西可能很好,甚至用GZip [] Stream包装它。)

@Robert comment is right. Java serialization is not intented for communication between different environments. More if you're working between JVM and Dalvik. So another framework would be more suitable (something with XML or JSON could be nice, even wrapping it with a GZip[]Stream).

这篇关于构造ObjectInputStream时出现EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆