今天遇到这样一个异常:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at Server.RequestProcessor.run(RequestProcessor.java:40)
at java.lang.Thread.run(Thread.java:662)
网上查了一下说:达了文件的末尾,程序却没有正常结束读取文件内容,
不是太理解什么意思,下面是出错的地方:
public void run() {
boolean flag = true; //是否不间断监听
    try{
   OnlineClientIOCache currentClientIOCache = new OnlineClientIOCache(
new ObjectInputStream(currentClientSocket.getInputStream()), 
new ObjectOutputStream(currentClientSocket.getOutputStream()));
while(flag){ //不停地读取客户端发过来的请求对象
//从请求输入流中读取到客户端提交的请求对象
Request request = (Request)currentClientIOCache.getOis().readObject();
System.out.println("Server读取了客户端的请求:" + request.getAction());

String actionName = request.getAction();   //获取请求中的动作
if(actionName.equals("userRegiste")){      //用户注册
registe(currentClientIOCache, request);
}else if(actionName.equals("userLogin")){  //管理员登录
login(currentClientIOCache, request);
}else if(actionName.equals("userLogin2")){  //用户登录
login2(currentClientIOCache, request);
}else if(actionName.equals("exit")){       //请求断开连接
flag = logout(currentClientIOCache, request);
}else if(actionName.equals("chat")){       //管理员发布信息
chat(request);
}else if(actionName.equals("chat2")){       //客户端聊天
chat2(request);
}else if(actionName.equals("add")){       //客户端聊天
add(request);
}else if(actionName.equals("agree")){       //客户端聊天
add1(request);
}
}
}catch(Exception e){
e.printStackTrace();
}
}

解决方案 »

  1.   

    看这源码/**
     * Peeks at (but does not consume) and returns the next byte value in
     * the stream, or throws EOFException if end of stream/block data has
     * been reached.
     */
    byte peekByte() throws IOException {
        int val = peek();
        if (val < 0) {
    throw new EOFException();
        }
        return (byte) val;
    }我觉得你是要对io流是否达到末尾有所判断才能避免这个EOF异常
      

  2.   

    你应该从Socket来监视是否有请求发送过来,当有Request到来时,才读取客户端发过来的请求对象。一般是writeObject几次,才readObject几次;
    所以下面做法肯定会出现EOF异常:
    while(flag){ //不停地读取客户端发过来的请求对象
    //从请求输入流中读取到客户端提交的请求对象
    Request request = (Request)currentClientIOCache.getOis().readObject();给个测试示例:public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    ByteArrayOutputStream bot = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bot);
    oos.writeObject(new Date());
    oos.writeObject(3.1415926);
    oos.writeObject(2011);
    oos.writeObject("Hello World");
    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bot.toByteArray()));
    while(true){
    try{
    Object obj = ois.readObject();
    System.out.println(obj);
    }catch (Exception e) {
    e.printStackTrace();
    ois.close();
    break;
    }
    }

    }
      

  3.   


    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                InputStream in = request.getInputStream();
                int bytesRead = 0;
                while ((bytesRead = in.read()) != -1) {
                    try {
                        bos.write(bytesRead);
                    } catch (Exception e) {
                    }
                }
                byte[] bt = bos.toByteArray();
    System.out.println(new String(bt, "GBK"));
                bos.close();
                in.close();