/java/

JAX-RS File Response

2015-08-22 22:34:26

Open pdf in browser

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.io.File;

@Path("/")
public class MyCtrl {

    @GET
    @Path("/")
    @Produces("application/pdf")
    public Response doTest() {

        Response.ResponseBuilder response = Response.ok(
                new File("file.pdf")
        );

        return response.build();
    }
}

Download file


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.io.File;

@Path("/")
public class MyCtrl {

    @GET
    @Path("/")
    @Produces("application/pdf")
    public Response doTest() {

        Response.ResponseBuilder response = Response.ok(
                new File("file.pdf")
        );

        response.header("Content-Disposition", "attachment; filename=file.pdf");
        return response.build();
    }
}