How to upload a file using http put in android?

I have a REST web service and android. Now I want to request Http Put using Android to call a web service. In my REST web service, if the user wants to do Http Put, he can request the following in the terminal:

curl -H "Content-Type: application / vnd.org.snia.cdmi.dataobject" -v -T / home / student1 / a.jpg http: // localhost: 8080 / user1 / folder / a.jpg

My question is how to install -T / home / student1 / a.jpg in android using HttpPut?

+5
source share
1 answer

Here is a snippet you can use:

File f = new File(...);
...
...
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut("http://mydomain.com/some/action");

MultipartEntity entity = new MultipartEntity();
entity.addPart("myFile", new FileBody(f));

httpPut.setEntity(entity);
HttpResponse response  = httpclient.execute(httpPut);
+8
source

All Articles