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

  • Home
  • SEARCH
  • 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 8639519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:01:35+00:00 2026-06-12T11:01:35+00:00

How to send file in POST-query using Apache HTTPComponents?

  • 0

How to send file in POST-query using Apache HTTPComponents?

  • 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-12T11:01:37+00:00Added an answer on June 12, 2026 at 11:01 am

    One possible solution is to generate HTTP headers by yourself, as explained here – http://en.wikipedia.org/wiki/MIME (see “Multipart messages”)

    I wrote such function. Maybe it is not well-written, but it works great.

    private static final String REQUEST_BOUNDARY = "somethingUniqueForYourQuery";
    
    private static String sendRequest(String url, String method, String params, File file, String fileField) {
        HttpURLConnection httpConn = null;
        StringBuilder httpContent = new StringBuilder();
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        byte[] fileContent = new byte[0];
        int fileSize = 0;
    
        // trying to read file
        if (file != null) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
    
                httpContent.append(twoHyphens + REQUEST_BOUNDARY + lineEnd);
                httpContent.append("Content-Disposition: form-data; name=\"" + fileField + "\"; filename=\"" + file.getName() + "\"" + lineEnd);
                httpContent.append(lineEnd);
    
                fileSize = fileInputStream.available();
                fileContent = new byte[fileSize];
                fileInputStream.read(fileContent, 0, fileSize);
                fileInputStream.close();
            }
            catch (Exception e){
                Log.d(DEBUG_TAG, "Exception occured: " + e.toString());
            }
        }
    
        // trying to perform request
        try {
            httpConn = (HttpURLConnection) new URL(url).openConnection();
    
            if (httpConn != null) {
                httpConn.setDoInput(true);
                httpConn.setDoOutput(true);
                httpConn.setUseCaches(false);
                httpConn.setConnectTimeout(CONNECTION_TIMEOUT_STRING);
                httpConn.setRequestMethod(method);
    
                if (file != null && httpContent.length() > 0) {
                    httpConn.setRequestProperty("Connection", "Keep-Alive");
                    httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + REQUEST_BOUNDARY);
    
                    DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
                    if (params != null) {
                        dos.writeBytes(params);
                    }
                    dos.writeBytes(httpContent.toString());
                    dos.write(fileContent, 0, fileSize);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + REQUEST_BOUNDARY + twoHyphens + lineEnd);
                    dos.flush();
                    dos.close();
                }
                else if (params != null) {
                    PrintWriter out = new PrintWriter(httpConn.getOutputStream());
                    out.print(params);
                    out.close();
                }
    
                httpConn.connect();
    
                int response = httpConn.getResponseCode();
                BufferedReader rd;
    
                if (httpConn.getErrorStream() == null) {
                    rd = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                } else {
                    rd = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
                }
    
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line + "\n");
                }
                if (rd != null) {
                    rd.close();
                }
                return sb.toString();
            } else {
                Log.d(DEBUG_TAG, "Connection Error");
            }
        }
        catch (Exception e){
            Log.d(DEBUG_TAG, "Exception occured: " + e.toString());
        }
        finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
        }
        return null;
    }
    
    • 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 file using the POST command available on almost
am trying send an array to php file using $_POST ,still always getting an
I need to send a file to a web service (ebridge) using their SendFile
I would like to send this textfield value to a php file using jQuery.
I'm trying to post some very simple data to a php file using jquery
I have to send .csv file to remote server through REST in rails. I
I need to send a file to MobileMe via Cocoa. I stumbled across a
I'm trying to send a file to the browser to download, and not having
I am trying to send a file to the browser in a Zend Framework
I'm trying to send a file in chunks to an HttpHandler but when I

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.