Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indicate the file name for saving to the browser from a REST service in Jersey?

i am trying to have a REST service return a zip file from the local harddrive . Following is what i am doing ,

@Path("/interface3/{Ent_id}/{esf_app_id}/{esf_app_ver}")
public class Interface3Mock {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces("application/zip")
    public Response  callInterface3_text(
            @PathParam("Ent_id") Integer entitlement_id,
            @PathParam("eapp_id") String eapp_id,
            @PathParam("eapp_ver") String eapp_ver) {
        File f = new File("D:\\Documentation\\Documentation.zip");

        String mt = new MimetypesFileTypeMap().getContentType(f);

        return Response.ok(f, mt).build();

    }
}

Now when i use the browser ie. Internet Explorer and key in the url http://localhost:9788/mockRESTServer/rest/interface3/123456/k123/l345 i see a file download dialog that says "Do you want to save the file l345`.

i want it to ask me for the zip download ie. D:\\Documentation\\Documentation.zip. But somehow it takes up the last parameter in the request URL.

like image 495
Som Bhattacharyya Avatar asked Jul 27 '11 20:07

Som Bhattacharyya


People also ask

What is the correct way to send a file from a REST web service to a client?

Using Postman to upload Files If you are using Postman to test your REST Services, all you need to do is create a New | HTTP request. From the HTTP Request UI: Enter the URL of the REST Service (f.e. http://localhost:8080/rest-file-manager/resr/file/upload ) Select POST as method.

How do I send a file through RESTful web services?

To attach a file, you must include it with the Body as form-data. Once you are in the Body → form-data fields, you must enter a KEY . This should be “file” or whichever value you specified in the @RequestPart(“[value]”) . After doing so, a dropdown will appear that gives you the option of Text or File.

What is MediaType Application_octet_stream?

A MediaType constant representing ""application/json"" media type. static String. APPLICATION_OCTET_STREAM. A String constant representing ""application/octet-stream"" media type.

How do I download from rest assured?

Download file using Rest Assured programRight-click on the target file (here, minions. zip). If you enter the above URL in the browser, the browser automatically starts downloading the file. So lets write the code to hit the above URL endpoint with our Rest Assured code.


1 Answers

return Response.ok(f, mt)
        .header("Content-Disposition", "attachment; filename=Documentation.zip")
        .build();

See How to set response header in JAX-RS so that user sees download popup for Excel?

like image 172
Matt Ball Avatar answered Oct 26 '22 23:10

Matt Ball