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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:35:37+00:00 2026-06-11T19:35:37+00:00

I am writing a uploader class for a site and in that, after uploading

  • 0

I am writing a uploader class for a site and in that, after uploading the files, I am reading the upload response from that site. If I didn’t read the response, then the file is not getting uploaded. My code is as follows,

   String charset = "UTF-8";


        File binaryFile = new File("C:\\TestVideoFile.flv");
        String boundary = Long.toHexString(System.currentTimeMillis());
        System.out.println(boundary);// Just generate some unique random value.
        String CRLF = "\r\n"; // Line separator required by multipart/form-data.
        URLConnection connection = new URL(UPLOAD_URL).openConnection();
        connection.setDoInput(true); 
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        PrintWriter writer = null;
        try {
            OutputStream output = connection.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
            writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
            writer.append("Content-Transfer-Encoding: binary").append(CRLF);
            writer.append(CRLF).flush();
            InputStream input = null;
            try {
                input = new FileInputStream(binaryFile);
                long filelen = binaryFile.length();
                System.out.println("Length : " + filelen);
                int dataRead = 0;
                byte[] buffer = new byte[1024];
                for (int length = 0; (length = input.read(buffer)) > 0;) {

                    output.write(buffer, 0, length);
                }
                System.out.println("Now only terminating the file write loop");
                output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException logOrIgnore) {
                        System.out.println(logOrIgnore);
                    }
                }
            }
            writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.

            // End of multipart/form-data.
            writer.append("--" + boundary + "--").append(CRLF);

            System.out.println("Sending username");
            // Send normal param.
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"user\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF);
            writer.append(username).append(CRLF).flush();
            System.out.println("Sending password");
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"password\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF);
            writer.append(password).append(CRLF).flush();


            System.out.println("Reading response from server");

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String k = "", tmp = "";
            while ((tmp = br.readLine()) != null) {
                System.out.println(tmp);
                k += tmp;
            }
            if (k.contains("Successfully")) {
                System.out.println("File Uploaded successfully into PutLocker :)");
                String downloadLink = parseResponse(k, "<link>", "</link>");
                System.out.println("Download Link : " + downloadLink);
            } else {
                System.out.println("Upload failed :(");
            }

          } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }

As you can see, I am writing the data into the server in the following line,

for (int length = 0; (length = input.read(buffer)) > 0;) {

                        output.write(buffer, 0, length);
                    }

But after this, I have to do the following,

 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String k = "", tmp = "";
                while ((tmp = br.readLine()) != null) {
                    System.out.println(tmp);
                    k += tmp;
                }

Why do I have to read the response from server to make the upload successful?

Can anybody explain on this?

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-11T19:35:39+00:00Added an answer on June 11, 2026 at 7:35 pm

    If you try to post something to the server and the connection get interrupted before you fully send your message, then the server will stop processing the incomplete request. Once the server is done receiving the request and you don’t wait to get its response, then you will never know if your request is sent successfully or not. Therefore, the URLConnection is designed to wait until you get the response.

    Another reason is that you may want build and configure your URLConnection first and then send it later when you want to by invoking the getInputStream or getResponseCode. You have more control of when you want the transaction to happen.

    You do not need to always getInputStream, just calling getResponseCode is good enough to complete the request. However, the whole input stream is still sent to your code but they are discarded.

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

Sidebar

Related Questions

I have some CSV files that I need uploaded to a site I'm writing
Im writing a pruning script, to delete content from my site that was uploaded
I'm writing a web application that allow user upload their files on the app.
We are using FTPWebRequest class in my project to upload PDF files. For uploading
I'm writing an uploader that has to be able to transmit files of any
I'm currently writing an upload class for uploading images. I do extension checks to
Writing htaccess that allows me to remove index.php from the URL can confuse search
I'm writing an iPad application that mimics a flash website I built. The site
I'm writing a small web app that will receive and parse tab-delimited text files
I have an app that I'm writing that takes files in a specific directory

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.