Reputation: 1101
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
Upvotes: 1
Views: 3869
Reputation: 76
Change The Following Line Of Code:
String urlString="http://localhost/uploads/upload.php";
to
String urlString="http://10.0.2.2:portno/uploads/upload.php";
Explanation:
Note here the IP Address 10.0.2.2 refers to the Localmachine's IP Address. Whereas localhost refer's to the emulator's IP itself. Also the "portno" Will Usually Be The Post No On Which Your Server Services Are Running Usually 80(By Default)..
HOPE This Helps..!! ;)
Upvotes: 0
Reputation: 6186
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.
Upvotes: 1
Reputation: 10462
In emulator, the SDCard is not having the read/write/execute permission and unfortunately there is no way to set these permissions for the emulator's SDCard. To test your code, I recommend to put your file in asset
of your application and use AssetManager
to get reference of that file and upload it.
By this way you can atleast test the upload algo and I am sure if that works, It will also work on actual device.
Upvotes: 0