我有Drugbean对象,必须插入数据库我使用对象输入流,以便当我将对象的数据插入数据库时从另一个类获取对象我得到java.io.EOFException
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int flag=0;
ObjectInputStream inputStream = null;
ObjectOutputStream outputStream = null;
DrugModel drugBean = new DrugModel();
List<DrugModel> itemFromClient = new ArrayList<DrugModel>();
List<String> itemToClient = new ArrayList<String>();
try {
inputStream = new ObjectInputStream(request.getInputStream());
while((drugBean = (DrugModel) inputStream.readObject())!=null){
flag = MakeConnection.insertDrugRecord(drugBean);
}
inputStream.close();
if (flag == 1) {
itemToClient.add("true");
outputStream = new ObjectOutputStream(response.getOutputStream());
outputStream.writeObject(itemToClient);
} else {
itemToClient.add("false");
outputStream = new ObjectOutputStream(response.getOutputStream());
outputStream.writeObject(itemToClient);
}
} catch (Exception e) {
e.printStackTrace();
}
cosole.error
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.medeil_plus.SyncDrugTable.doPost(SyncDrugTable.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
EOFException
时, readObject()
是正常的。当你到达流的末尾时会发生这种情况。
您的错误在于假设它在流结束时返回null。它没有。它可以在您写入null时返回null。
因此,您的阅读循环应为while (true)
,并明确表示EOFException
将其退出。