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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:17:58+00:00 2026-05-20T17:17:58+00:00

hi friends iam trying to upload a file from the android device to the

  • 0

hi friends iam trying to upload a file from the android device to the server.i tried it with emulator and localhost but it does not working.
i am using wamp server in local system . my android code for upload file is given below. i need uploading file from emulator to localsystem

upload.java

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName =FileName.getText().toString();
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
//String responseFromServer = "";
String urlString="http://localhost/uploads/upload.php";

try
{

FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
 // open a URL connection to the Servlet
 URL url = new URL(urlString);

 conn = (HttpURLConnection) url.openConnection();

 conn.setDoInput(true);

 conn.setDoOutput(true);

 conn.setUseCaches(false);

 conn.setRequestMethod("POST");
 conn.setRequestProperty("Connection", "Keep-Alive");
 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
 dos = new DataOutputStream( conn.getOutputStream() );
 dos.writeBytes(twoHyphens + boundary + lineEnd);
 dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
 dos.writeBytes(lineEnd);

 bytesAvailable = fileInputStream.available();
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 buffer = new byte[bufferSize];

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

 dos.writeBytes(lineEnd);
 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

 Log.e("Debug","File is written");
 fileInputStream.close();
 dos.flush();
 dos.close();
 Toast.makeText(Upload.this, "Files have been uploaded successfully",Toast.LENGTH_SHORT).show();
}
catch (MalformedURLException ex)
{
     Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
     Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}

try {
      inStream = new DataInputStream ( conn.getInputStream() );
      String str;

      while (( str = inStream.readLine()) != null)
      {
           Log.e("Debug","Server Response "+str);
      }
      inStream.close();

}
catch (IOException ioex)
{
     Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}

here is my php code

upload.php

<?php

 $target_path = "uploads/";

 $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
 } 

 else{
      echo "There was an error uploading the file, please try again!";
   }
   ?>

i have a folder named uploads in the www directory of wamp folder.and i need to store the uploaded file in this folder.

but when i run the android program it shows error message as

"java.net.Connect Exception: localhost/127.0.0.1:80 - Connection refused"

can anybody give me a solution for this and let me know my coding are enough to achieve my target. 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-05-20T17:17:59+00:00Added an answer on May 20, 2026 at 5:17 pm

    user664525
    From the "java.net.Connect Exception: localhost/127.0.0.1:80 - Connection refused" it seems that Android is trying to refer to its own localhost not your server’s address.

    You need to provide the IP address of your server’s machine(in your case your own system’s say 192.168.40.24) not the localhost in your code.

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

Sidebar

Related Questions

friends, i am trying to upload file to php server using following tutorial http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
Hi friends, I am trying to get the string data and image data from
friends, i am trying to check internetconnectivity in android and using following code final
friends, i am trying to display image from gallery now i want to put
I have to find friends of friends (not my friends) from an sql table
I am trying to get the list of specific user`s friends from twitter. This
I am trying to fake a file upload without actually using a file input
friends i am trying to work with predesigned visual studio dotNet projects. so i
Problem I am trying to get friends_location from the Facebok Graph API. I am
I am trying to create a database of users with connection between users (friends

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.