views:

516

answers:

3

I want to send the contents of a file as part of a http request using Apache HttpClient and I could not figure out how to pass on the file contents in the request body.

A: 

Check out FilePart and related.

Here's the sample.

Hank Gay
+2  A: 

You didn't specify the format....

Most likely, you want to send a POST request, the contents will be multipart/form-data MIME type. This emulates what a browser sends from an <INPUT type="file" ...> form element. This requires some pretty sophisticated parsing on the server side to extract the multiple parts from the body and correctly extract the file data from the other fields (if any). Fortunately, commons-fileupload does this perfectly. The first answer regarding FilePart is exactly right.

Alternatively, you could simply post the raw contents of a file as the body of the request by using an InputStreamRequestEntity. This may be much simpler if you're writing your own server side to receive the data. The server side is as simple as streaming the request's InputStream to disk. I use this technique for uploads with Google Gears.

Mark Renouf
Thanks. InputStreamRequestEntity worked just fine for me.
Vijay Dev
A: 

Some code for it here: Post Method Snippet in httpclient 4

php html