Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a file in Android from a mobile device to server using http?

Tags:

android

http

post

In android, how do I send a file(data) from a mobile device to server using http.

like image 906
Subrat Avatar asked Nov 08 '10 18:11

Subrat


People also ask

How do I transfer files using HTTP?

To use the File Transfer Using HTTP feature, you may need to specify a username and password for the HTTP connections for those servers that require a username and password to connect. Commands are also available to specify custom connection characteristics, although default settings can be used.

What is HTTP server in Android?

GitHub - piotrpolak/android-http-server: A complete zero-dependency implementation of a web server and a servlet container in Java with a sample Android application. Product.

How do I upload a file using Httpurlconnection?

To upload you file along with parameters. NOTE : put this code below in non-ui-thread to get response. String charset = "UTF-8"; String requestURL = "YOUR_URL"; MultipartUtility multipart = new MultipartUtility(requestURL, charset); multipart. addFormField("param_name_1", "param_value"); multipart.


2 Answers

Easy, you can use a Post request and submit your file as binary (byte array).

String url = "http://yourserver"; File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),         "yourfile"); try {     HttpClient httpclient = new DefaultHttpClient();      HttpPost httppost = new HttpPost(url);      InputStreamEntity reqEntity = new InputStreamEntity(             new FileInputStream(file), -1);     reqEntity.setContentType("binary/octet-stream");     reqEntity.setChunked(true); // Send in multiple parts if needed     httppost.setEntity(reqEntity);     HttpResponse response = httpclient.execute(httppost);     //Do something with response...  } catch (Exception e) {     // show error } 
like image 161
Emmanuel Avatar answered Sep 19 '22 05:09

Emmanuel


This can be done with a HTTP Post request to the server:

HttpClient http = AndroidHttpClient.newInstance("MyApp"); HttpPost method = new HttpPost("http://url-to-server");  method.setEntity(new FileEntity(new File("path-to-file"), "application/octet-stream"));  HttpResponse response = http.execute(method); 
like image 26
DaGGeRRz Avatar answered Sep 19 '22 05:09

DaGGeRRz