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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:43:44+00:00 2026-06-08T12:43:44+00:00

i know that there are similar questions on Stackoverflow, but they did not solve

  • 0

i know that there are similar questions on Stackoverflow, but they did not solve my problem. I want to upload a file from android to a local apache server, but gain a ‘connection refused’ error.

I’am using a physical device with Android 3.2 and MAMP on Mac OS X Lion (Apache on Port 80). I also added the INTERNET permission to the manifest.

Here is the code:

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(new File(
                "/mnt/sdcard/somefile.xml"));
    } catch (FileNotFoundException e) {
        ...
    }

    URL url = null;
    try {
        url = new URL("http://192.168.x.xxx:80/index.php");
    } catch (MalformedURLException e) {
        ...
    }

    try {
        // Open a HTTP connection to the URL
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                + SOMEFILE + "" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1000;
        // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bytesAvailable];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesAvailable);
            bytesAvailable = fileInputStream.available();
            bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
        }

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();
        dos.flush();
        dos.close();
    } catch (IOException e) {
        ...
    }

The Exception:

07-24 14:48:43.280: E/App(5802): failed to upload file '/mnt/sdcard/somefile.xml' to   
server 'http://192.168.x.xxx:80/index.php'
07-24 14:48:43.280: E/App(5802): java.net.ConnectException: /192.168.x.xxx:80 -   
Connection refused
  • 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-08T12:43:46+00:00Added an answer on June 8, 2026 at 12:43 pm

    the code seems to be ok. The emulator can connect without problems. There was a problem with the router firewall

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

Sidebar

Related Questions

I know that on stackoverflow there are many similar questions but mine is a
I know there are other questions that have similar issues, but I have read
I know that there are many similar questions posted, but none of them refers
I know that there are plenty of similar questions on stack overflow. But the
I know that there are similar questions which are already answered, but I am
I know that there are similar questions on stackoverflow and i read then all.
I know that there are a few similar questions and I googled a lot
I know that there are lots of examples out there on this, but they
I know that there can be multiple values for an email, but I'm not
I know that there are many Delphi database related questions available, but I'm considering

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.