Parameswar
Parameswar

Reputation: 2079

Downloading a file using a REST POST call in java

I have a REST service which returns a file when it is called via a POST call with an XML file as the parameter. My goal is to access the service using a client (a simple httppost call in a java class). So far, I am doing as below:

    DefaultHttpClient defaultHtppclient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("resturl");
    StringEntity input = new StringEntity("input xml file ");
    input.setContentType("application/xml");
    postRequest.setEntity(input);
    HttpResponse response = defaultHtppclient.execute(postRequest);                           

I am getting the contents of the file when I convert the response using :

String content = EntityUtils.toString(response.getEntity());

But I am struggling to download the file as such from the java class. When I trigger the URL in Firefox HTTP resource test. I am getting the headers as:

Content-Disposition: attachment; filename = filenameFromserver
Content-Type: application/octet-stream

Is there any way to download the file as such from the client ?

Upvotes: 0

Views: 2984

Answers (1)

dardo
dardo

Reputation: 4970

The call you're making is expecting application/xml, the server is sending you an octet-stream.

You'll have to read in the bytes it's sending you and write them out to the file that you want. I'd recommend searching around on how to read in an octet stream.

For an example take a look at this post:

Reading binary file from URLConnection

Upvotes: 2

Related Questions