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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:25:08+00:00 2026-06-18T13:25:08+00:00

I am trying to write a program in java which is able to download

  • 0

I am trying to write a program in java which is able to download a file from a URL. I want to do this without using an URLConnection, instead i am just using TCP sockets. I have succeeded in sending the GET request and picking up the server’s response, but i can’t seem to get my head around saving the file from the response without the http-header(just the file).

import java.net.*;
import java.io.*;

public class DownloadClient {
    public static void main(String[] args) {
        try {
            if (args.length != 3) {
                System.out.println(
                    "Use: java DownloadClient <host> <port> <filename/path>"
                );
            } else {
                // Sorting out arguments from the args array
                String host;
                int port; 
                String filename;
                if (args[0].charAt(args[0].length()-1) == '/') {
                    host = args[0].substring(0,args[0].length()-1);
                } else {
                    host = args[0];
                }
                port = Integer.parseInt(args[1]);
                if (args[2].charAt(0) == '/') {
                    filename = args[2];
                } else {
                    filename = "/"+args[2];
                }

                Socket con = new Socket(args[0], Integer.parseInt(args[1]));

                // GET request
                BufferedWriter out = new BufferedWriter(
                    new OutputStreamWriter(con.getOutputStream(), "UTF8")
                );
                out.write("GET "+filename+" HTTP/1.1\r\n");
                out.write("Host: "+host+"\r\n");
                out.write("User-Agent: Java DownloadClient\r\n\r\n");
                out.flush();

                InputStream in = con.getInputStream();
                BufferedReader = 
                OutputStream outputFile = new FileOutputStream(
                    filename.substring(filename.lastIndexOf('/')+1)
                );
                byte[] buffer = new byte[1024];
                int bytesRead = 0;

                while((bytesRead = in.read(buffer)) > 0) {
                    outputFile.write(buffer, 0, bytesRead);
                    buffer = new byte[1024];
                }

                outputFile.close();
                in.close();
                con.close();
            }
        } catch (IOException e) {
            System.err.println(e); 
        }
    }
}

I guess that i should somehow look for \r\n\r\n as it indicates the empty line just before the content begins. So far this program creates a file which contains all of the http-response.

  • 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-18T13:25:09+00:00Added an answer on June 18, 2026 at 1:25 pm

    The recommended way to do this is to NOT try to talk to a web server using a plain Socket. Use one of the existing client-side HTTP stack; e.g. the standard HttpUrlConnection stack or the Apache HttpClient stack.

    If you insist on talking using a plain socket, then it is up to you to process / deal with the “Header” lines in any response … and everything else … in accordance with the HTTP specification.

    I guess that I should somehow look for \r\n\r\n as it indicates the empty line just before the content begins.

    Yup …

    And you also potentially need to deal with the server sending a compressed response, an response using an unexpected character set, a 3xx redirect, and so on.

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

Sidebar

Related Questions

I am trying to write a simple java program which returns me all the
I am trying to write a Java program or Hadoop Pig script which will
i am trying to write a program in java (on linux) using RandomAccessFile class
I am trying at write a java program that hits a url over ssl,
I am trying to write a Java program that reads an input file consisting
I have been trying to write a Java program which connects to mysql db
I've been trying to write a program in java that can download a number
I'm trying to write a simplest java web server program following an instruction which
I am trying to write a program in Java, which would collect a specified
I am trying to write a small java program that will accept a file

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.