新手求教,下面代码会报 invalid stream header异常,让我很头疼,不知道什么原因
package com.fiveTwo.Imfor.server;
import
java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import com.fiveTwo.Imfor.commonClass.Datas;
import com.fiveTwo.Imfor.commonClass.SysConstants;
import com.fiveTwo.Imfor.commonClass.Tools;
import com.fiveTwo.Imfor.entity.Student;
import com.fiveTwo.Imfor.entity.Teacher;
public class ServerContextListener extends Thread {
public static void main(String[] args) {
new ServerContextListener().start();
}
public void run(){
System.out.println("服务器端启动!");
ServerSocket serversocket = null;
Socket socket =null;
ObjectOutputStream objectOutputStream=null;
ObjectInputStream objectInputStream=null;
try {
//创建服务器Socket对象
serversocket = new ServerSocket(Integer.parseInt(Tools.getValue(SysConstants.SYS_LISTENERPORT)));
while (true) {
//创建客户端Socket对象
socket = serversocket.accept();
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
//获取客户端请求的操作
Datas datas = (Datas) objectInputStream.readObject();
// 创建服务器的服务类
ServerService serverService = new ServerService();
//判断客户端请求的对象
if (datas.getFlag().equals(SysConstants.SYS_LOGIN)) {
// 登录
String username = datas.getTeacher().getUserName();
String password = datas.getTeacher().getPassWord();
// 调用业务方法
Teacher teacher = serverService.doLogin(username, password);
if (teacher == null) {
datas.setFlag(SysConstants.SYS_ERROR);
} else {
datas.setFlag(SysConstants.SYS_SUCCESS);
datas.setTeacher(teacher);
}
}else{
if(datas.getFlag().equals(SysConstants.SYS_FIND)){
//查询
int teacherId=datas.getTeacher().getId();
List<Student> studentList=serverService.findStudentByTeacher(teacherId);
datas.setStudentList(studentList);
}else if(SysConstants.SYS_UPDATE.equals(datas.getFlag())){
//修改
Student student =datas.getStudent();
serverService.updateStudent(student);
}else if(SysConstants.SYS_DELETE.equals(datas.getFlag())){
//删除
int stuId=datas.getStudent().getStuId();
serverService.deleteStudent(stuId);
}
datas.setFlag(SysConstants.SYS_SUCCESS);
}
objectOutputStream.writeObject(datas);
objectOutputStream.flush();
socket.shutdownOutput();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
objectInputStream.close();
objectOutputStream.close();
socket.close();
// serversocket.close();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("服务器终止!");
}
}
控制台输出的信息:
服务器端启动!
java.io.StreamCorruptedException: invalid stream header: 64000000
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at com.fiveTwo.Imfor.server.ServerContextListener.run(ServerContextListener.java:35)
Exception in thread "Thread-0"
java.lang.NullPointerException at com.fiveTwo.Imfor.server.ServerContextListener.run(ServerContextListener.java:84)
------解决方案--------------------
你看你的控制流是不是有问题。有些流无效