title: java.io.EOFException when sending serialized data to a servlet
body: <p>I'm tring to upload from my Java local application an object that will include a file to a server. My plan is that a servlet running on tomcat will get the object using the <code>ObjectInputStream</code> in the <code>doGet</code> method. But I get an <code>EOFE</code>xception`.</p>
  
  <p>Here is the client code</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  public class Client {
      public static void main(String[] args) throws IOException {
          FileInputStream inputStream = new FileInputStream("c:\\rafi.txt");
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          byte[] buffer = new byte[1024];
          int n = 0;
          while (-1 != (n = inputStream.read(buffer))) {
              output.write(buffer, 0, n);
          }
          inputStream.close();
          File2 c2 = new File2(buffer);
          URL url = new URL("http://localhost:8080/servertest/Server");
          URLConnection cnx = url.openConnection();
          cnx.setDoInput(true);
          cnx.setDoOutput(true);
          cnx.setRequestProperty("Content-Type", "application/octet-stream");
          InputStream in = cnx.getInputStream();
          OutputStream out = cnx.getOutputStream();
          cnx.connect();
          ObjectOutputStream oos = new ObjectOutputStream(out);
          oos.writeObject(c2);
          oos.flush();
          oos.close();
          ObjectInputStream ois = new ObjectInputStream(in);
          boolean readBoolean = ois.readBoolean();
          System.out.println(readBoolean);
          ois.close();
          in.close();
          out.close();
      }
  }
  </code></pre>
  
  <p>here is the server's servlet</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  @WebServlet("/Server")
  public class Server extends HttpServlet {
      private static final long serialVersionUID = 1L;
      public Server() {
          super();
      }
      protected void doGet(HttpServletRequest req, HttpServletResponse res)
              throws ServletException, IOException {
          InputStream in = req.getInputStream();
          OutputStream out = res.getOutputStream();
          ObjectOutputStream oos = new ObjectOutputStream(out);
          ObjectInputStream ois = new ObjectInputStream(in);
  
          File2 data_in;
          try {
              data_in = (File2) ois.readObject();
              byte[] a = new byte[data_in.mybytearray.length];
              System.arraycopy(data_in.mybytearray, 0, a, 0,data_in.mybytearray.length);
              System.out.println(a.toString());
              oos.writeBoolean(true);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              oos.writeBoolean(false);
          }
          finally{
              oos.close();
              }
  
          res.setContentType("java-internal/" + File2.class.getName());
          in.close();
      }
  }
  </code></pre>
  
  <p>When I debug the server side and run the client I get the exception in this row</p>
  
  <pre><code>ObjectOutputStream oos = new ObjectOutputStream(out);
  </code></pre>
  
  <p>This is the error I get</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [test1.Server] in context with path [/servertest] threw exception
  java.io.EOFException
      at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
      at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
      at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source)
      at test1.Server.doGet(Server.java:38)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>I saw this <a href="https://stackoverflow.com/questions/4817360/eofexception-when-reading-serialized-object-in-servlet">question</a>, but i't didn't help me. I'm using tomcat 7.</p>