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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:58:07+00:00 2026-05-18T19:58:07+00:00

I am trying to upload files using Java URL class and I have found

  • 0

I am trying to upload files using Java URL class and I have found a previous question on stack-overflow which explains very well about the details, so I try to follow it. And below is my code adopted from the sniplet given in the answer.

My problem is that if I don’t make a call to one of connection.getResponseCode() or connection.getInputStream() or connection.getResponseMessage() or anything which is related to reponse from the server, the request will never be sent to server. Why do I need to do this? Or is there any way to write the data without getting the response?

P.S. I have developed a server-side uploading servlet which accepts multipart/form-data and save it to files using FileUpload. It is stable and definitely working without any problem so this is not where my problem is generated.

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.IOUtils;

public class URLUploader {

    public static void closeQuietly(Closeable... objs) {
        for (Closeable closeable : objs) {
            IOUtils.closeQuietly(closeable);
        }
    }

    public static void main(String[] args) throws IOException {
        File textFile = new File("D:\\file.zip");
        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

        HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8080/upslet/upload").openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        OutputStream output = connection.getOutputStream();

        PrintWriter writer = new PrintWriter(output, true);
        // Send text file.
        writer.println("--" + boundary);
        writer.println("Content-Disposition: form-data; name=\"file1\"; filename=\"" + textFile.getName() + "\"");
        writer.println("Content-Type: application/octet-stream");
        FileInputStream fin = new FileInputStream(textFile);
        writer.println();
        IOUtils.copy(fin, output);
        writer.println();
        // End of multipart/form-data.
        writer.println("--" + boundary + "--");
        output.flush();
        closeQuietly(fin, writer, output);
        // Above request will never be sent if .getInputStream() or .getResponseCode() or .getResponseMessage() does not get called.
        connection.getResponseCode();
    }
}
  • 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-18T19:58:08+00:00Added an answer on May 18, 2026 at 7:58 pm

    HttpURLConnection has a bunch of setters that affect the request, so it needs to know when you’re done building the request so that it can send it off. The way it does this is by waiting until you actually ask for something back from the request, like the InputStream. (This is arguably a design flaw.) This is also why the setters throw an IllegalStateException “if already connected” — once you call a getter the connection is established and it’s too late to call a setter.

    You should be able to call getInputStream() and then just close() it without actually reading from it.

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

Sidebar

Related Questions

I am trying to upload files using Java URL class and I have found
I am trying to upload files using the FileReference class. Files >2MB all work
I am trying to upload files via FTP using the following script. The file
I'm trying to upload files to the blobstore in my Google App without using
I am using CakePHP 2.1 and trying to upload files into server. I can
I am using railwayjs and I am trying to upload files. I am doing
I am using cakephp 2.1 and i am trying to upload files and how
I'm trying to upload pdf files in the server. And i;m using the following
I'm trying to upload files to server using Uploadify script and then show them
I am getting strange behaviour when trying to upload files, using jQuery multi-file plugin,

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.