java.io.StreamCorruptedException: invalid stream header: 异常

使用java的对象流出现java.io.StreamCorruptedException: invalid stream header异常

原因是数据发送端发送对象到接收端

接收端对于同一个输入流创建了不同的对象输入流,而后用不同的对象输入流进行接收

下面模拟异常的发送

传输的对象:

 

public class User implements Serializable{
	private String name;
	private int age;
	public User(String name, int age){
		this.name = name;
		this.age = age;
	}
	public String GetName(){
		return this.name;
	}
	public int GetAge(){
		return this.age;
	}
}

 

客户端程序:

 

public class Client {
	public static void main(String[] args) throws Exception {
		Socket client = new Socket("127.0.0.1", 1234);
		ObjectOutputStream objOutput = new ObjectOutputStream(client.getOutputStream());
		//用同一输出流发送两个对象
		User user = new User("小明", 20);
		objOutput.writeObject(user);
		User user1 = new User("小红", 10);
		objOutput.writeObject(user1);
		client.close();
	}
}

 

 

 

服务端:

 

public class Server {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		ServerSocket serverSocket = new ServerSocket(1234);
		
		System.out.println("start. . .");
		Socket clientSocket = serverSocket.accept();
		
		//用clientSocket的输入流创建对象输入流objInput
		ObjectInputStream objInput = new ObjectInputStream(clientSocket.getInputStream());
		User user = (User)objInput.readObject();
		System.out.println(user.GetName());
		System.out.println(user.GetAge());
		
		//用clientSocket的输入流创建对象输入流objInput2
		ObjectInputStream objInput2 = new ObjectInputStream(clientSocket.getInputStream());
		user = (User)objInput2.readObject();
		System.out.println(user.GetName());
		System.out.println(user.GetAge());	
		
		
		clientSocket.close();
		serverSocket.close();
	}

}


服务端用网络输入流创建了两个不同的对象输入流,这时就会产生异常,如果用同一个对象输入流接收就不会产生。

 

 

java.io.StreamCorruptedException: invalid stream header: 41434544

04-17

java.io.StreamCorruptedException: invalid stream header: 41434544rn at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)rn at java.io.ObjectInputStream.(ObjectInputStream.java:280)rn at org.hibernate.util.SerializationHelper$CustomObjectInputStream.(SerializationHelper.java:252)rn at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)rn at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)rn at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)rn at org.hibernate.type.SerializableType.get(SerializableType.java:39)rn at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)rn at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)rn at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)rn at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2091)rn at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)rn at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)rn at org.hibernate.loader.Loader.getRow(Loader.java:1206)rn at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)rn at org.hibernate.loader.Loader.doQuery(Loader.java:701)rn at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)rn at org.hibernate.loader.Loader.loadEntity(Loader.java:1864)rn at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)rn at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)rn at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3039)rn at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)rn at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)rn at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)rn at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)rn at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)rn at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:879)rn at org.hibernate.impl.SessionImpl.get(SessionImpl.java:816)rn at org.hibernate.impl.SessionImpl.get(SessionImpl.java:809)rn at dao.ProjectDao.getProject(ProjectDao.java:20)rn at action.QueryAction.execute(QueryAction.java:32)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Thread.java:619) 论坛

求助: java.io.StreamCorruptedException: invalid stream header: 32303134

12-08

最近,在做一个j2ee的开发,遇到了一个问题,耽误了我一天的时间,不知道该怎么解决,求助大家!rnrn情况如下:rn开发IDE:Intelij IDEArn框架 :使用SSH框架开发。 spring 最新版 4.1.2 ,struts 2.3,Hibernate 4.3.7rnrn问题点:[code=java]rn Transaction tx = sess.beginTransaction();rn String sql = "from NewsType ";rn Query q = sess.createQuery(sql);rn tx.commit();rn result = q.list();[/code] rn问题就在最后一句,我debug时,发现q中的size==0,而且只要q.list()就出问题rnrn具体错误: Caused by: java.io.StreamCorruptedException: invalid stream header: 32303134rnrn整体Exception如下:rn[code=java]rnorg.hibernate.type.SerializationException: could not deserializern at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)rn at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)rn at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:155)rn at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:130)rn .......rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)rn at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)rn at java.lang.Thread.run(Thread.java:745)rn Caused by: java.io.StreamCorruptedException: invalid stream header: 32303134rn [code=java]at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)rn at java.io.ObjectInputStream.(ObjectInputStream.java:299)rn at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.(SerializationHelper.java:328)rn at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.(SerializationHelper.java:318)rn at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:237)rn ... 103 morern[/code] 论坛

没有更多推荐了,返回首页