Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:27:52+00:00 2026-05-22T11:27:52+00:00

I’m tring to upload from my Java local application an object that will include

  • 0

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 ObjectInputStream in the doGet method. But I get an EOFException`.

Here is the client 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();
    }
}

here is the server’s servlet

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();
    }
}

When I debug the server side and run the client I get the exception in this row

ObjectOutputStream oos = new ObjectOutputStream(out);

This is the error I get

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.<init>(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)

I saw this question, but i’t didn’t help me. I’m using tomcat 7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:27:53+00:00Added an answer on May 22, 2026 at 11:27 am
    InputStream in = cnx.getInputStream();
    OutputStream out = cnx.getOutputStream();
    

    The URLConnection#getInputStream() will immediately send the HTTP request to the server in order to retrieve the response body. The way as you’ve written the code, this thus takes place before you’ve written any bit to the HTTP request body. Hence the EOFException on the server side.

    You need to ask for the HTTP response body by URLConnection#getInputStream() after you’ve written the necessary data to the HTTP request body. Here’s a rewrite:

    URLConnection connection = new URL("http://localhost:8080/servertest/Server").openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "application/octet-stream");
    
    ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
    oos.writeObject(c2);
    oos.close();
    
    ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
    boolean readBoolean = ois.readBoolean();
    ois.close();
    
    System.out.println(readBoolean);
    

    Also, since you’re basically sending a HTTP POST request, you need to handle this in the servlet’s doPost() method rather than the doGet() method.


    Unrelated to the concrete problem: this isn’t really the best way of sending files over HTTP. While it might work, this is very tight coupled to Java serialization mechanism. I’d suggest to send HTTP multipart/form-data requests instead. This can be achieved by Apache HttpComponents Client on the client side and Apache Commons FileUpload on the server side. This way the servlet is reuseable for other purposes, such as a HTML form with <input type="file"> in the front. Also the client can this way be reused to upload files to other HTTP websites.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.