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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:42:42+00:00 2026-05-26T22:42:42+00:00

I was playing around with Java sockets and I was trying to trasnfer files

  • 0

I was playing around with Java sockets and I was trying to trasnfer files from a server to client, however, when they get transfer they are corrupted. This is the code from the server:

DataInputStream input;
DataOutputStream ouput;
//these two variable are initialized somewhere else in the code.
private void downloadFile() {
    try {
        String fileName= input.readUTF();
        File f = new File(path + fileName);
        size= f.length();
        file= new FileInputStream(path+ fileName);
        ouput.writeLong(size);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = file.read(buffer)) > 0) {
            output.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

on the client side:

public void downloadFile(String fileName) {
    try {
        this.client= new Socket(ip,port);
        DataInputStream input= new DataInputStream(this.client.getInputStream());
        DataOutputStream ouput= new DataOutputStream(this.client.getOutputStream());

        output.writeUTF("DOWNLOAD");
        output.writeUTF(fileName);

        File f = new File(path+ fileName);
        file = new FileOutputStream(f);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = input.read(buffer)) > 0) {
            file.write(buffer, 0, len);
        }
        file.flush();
        file.close();
        this.client.close();
    } catch (Exception e) {
        System.out.println("something went wrong");
    }
}

I dont know what am I doing wrong, the file gets completely transfer but not correctly.

  • 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-26T22:42:43+00:00Added an answer on May 26, 2026 at 10:42 pm

    on the server:

    ouput.writeLong(size);
    

    you dont seem to handle this on the client side, you just append it to the downloaded file as if it was part of the binary data.

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

Sidebar

Related Questions

Just playing around with java trying to learn it etc. Here is my code
I'm just learning c++ coming from a Java background. Just playing around with simple
Extremely new to Java an just playing around with it. I'm trying to add
I am playing around with Twisted and while trying to rewrite an old java
I'm playing around with some code katas and trying to get a better understanding
I'm playing around with Java's reflection API and trying to handle some fields. Now
I have been playing around with node.js, and coming from a Java background, I
I am playing around with Android Sockets and I currently I am trying to
Java Day 1 from c#. Just playing around with java (just for fun) and
Completely new to java and I have been playing around with regex in a

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.