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 8525437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:58:40+00:00 2026-06-11T07:58:40+00:00

I would like to send file from client to server and be able do

  • 0

I would like to send file from client to server and be able do it again in the future.

So my client connect to server and upload file, ok – it works but it hangs at the end..

so here is my code in client, the server side is quite similar.

private void SenderFile(File file) {


    try {

            FileInputStream fis = new FileInputStream(file);
            OutputStream os = socket.getOutputStream();

            IoUtil.copy(fis, os);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

IoUtils found on Stack 🙂

public static class IoUtil {

    private final static int bufferSize = 8192;

    public static void copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[bufferSize];
        int read;

        while ((read = in.read(buffer, 0, bufferSize)) != -1) {
            out.write(buffer, 0, read);
        }
        out.flush();
    }
}

Explanation: my client has a socket connected to server, and I send any file to him.
My server download it but hangs at the end because he is listening for more infromation.
If I choose another file, my server will download new data to the existing one.

How could I upload any file to server, make my server work on and be able download another one file properly?

ps. If I add to ioutil.copy at the end of function out.close my server will work on but the connection will be lost. I do not know what to do :{


After update:
Client side:

private void SenderFile(File file) {
    try {

        FileInputStream fis = new FileInputStream(file);
        OutputStream os = socket.getOutputStream();

        DataOutputStream wrapper = new DataOutputStream(os);
        wrapper.writeLong(file.length());
        IoUtil.copy(fis, wrapper);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Server side (thread listening for any message from client):

public void run() {
    String msg;
    File newfile;

    try {
        //Nothing special code here

        while ((msg = reader.readLine()) != null) {


            String[] message = msg.split("\\|");
            if (message[0].equals("file")) {//file|filename|size

                String filename = message[1];
                //int filesize = Integer.parseInt(message[2]);

                newfile = new File("server" + filename);


                InputStream is = socket.getInputStream();
                OutputStream os = new FileOutputStream(newfile);

                DataInputStream wrapper = new DataInputStream(is);

                long fileSize = wrapper.readLong();
                byte[] fileData = new byte[(int) fileSize];
                is.read(fileData, 0, (int) fileSize);
                os.write(fileData, 0, (int) fileSize);


                System.out.println("Downloaded file");
            } else

                //Nothing special here too
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

Ok, now I can download file – still once, another one is downloaded but unable to read. For example, second time I want send by client a file.png. I got it on server, but this file is not possible to view.
Thanks in advance 🙂

  • 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-06-11T07:58:41+00:00Added an answer on June 11, 2026 at 7:58 am

    You need to make your server able to differentiate files. The easiest way is to tell in advance how many bytes the receiving end should expect for a single file; this way, it knows when to stop reading and wait for another one.

    This is what the SenderFile method could look like:

    private void SenderFile(File file)
    {
        try
        {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = socket.getOutputStream();
    
            DataOutputStream wrapper = new DataOutputStream(os);
            wrapper.writeLong(file.length());
            IoUtil.copy(fis, wrapper);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    

    And this is what the ReceiveFile method could look like:

    // the signature of the method is complete speculation, adapt it to your needs
    private void ReceiveFile(File file)
    {
        FileOutputStream fos = new File(file);
        InputStream is = socket.getInputStream();
        DataInputStream wrapper = new DataInputStream(is);
    
        // will not work for very big files, adapt to your needs too
        long fileSize = wrapper.readLong();
        byte[] fileData = new byte[fileSize];
        is.read(fileData, 0, fileSize);
        fos.write(fileData, 0, fileSize);
    }
    

    Then don’t close the socket.

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

Sidebar

Related Questions

I would like to send a struct from my client program to a server
I have a byte[] with the contents of file. I would like to send
I would like to send the message from HelloWorldLayer, and receive it in ScoreLayer,
I would like to send out an update of my apk, but i did
I'm writing an obj-c app and would like to upload a binary file a
I have a backup log file from robocopy and would like to take last
I want to upload files from a client location to a server. At present,
I have a WCF service where I would like to send a log file
I would like to send a javascript array to be processed by a method
I would like to send a html string with a GET request like this

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.