0

Can someone provide me a demo of sending pdf files as response ?

Endpoint is

@GET
@Path("/PDFiles")
@WebMethod(operationName = "PDFiles")
public Response pdfiles() {

    LOGGER.info("Getting FPodAUMFile.");
    return dao.getPDFfiles(CacheKeys.pdffile);
}

DAO would be

public Response getPDFfiles(String pdffile) {

        File file_pdf = new File("D:/pdffile.pdf");

// HELP ME SEND THIS PDFFILE.PDF AND COMPLETE THE CODE HERE

}

MTOM Simplifies the way it is sent. Can someone elaborate on using MTOM also ?

kss
  • 169
  • 1
  • 11

1 Answers1

1

You need to specify Content-Disposition header in your response and write the file into the response entity. So for e.g.:

public Response getPDFfiles(String pdffile) {
    File filePdf = new File("D:/pdffile.pdf"); //You'll need to convert your file to byte array.
    ContentDisposition contentDisposition = new ContentDisposition("attachment;filename=pdffile.pdf");
    return Response.ok(
            new StreamingOutput() {
                @Override
                public void write(OutputStream outputStream) throws IOException, WebApplicationException {
                    outputStream.write(/* Your file contents as byte[] */);
                }
            })
            .header("Content-Disposition", contentDisposition.toString())
            .header("Content-Type", "application/pdf")
            .build();
}

How to convert a file to byte[] can by found here.

Community
  • 1
  • 1
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • Hi thanks, but I am having an error. Service class method pdfiles part {http:///}PDFilesResponse cannot be mapped to schema. Check for use of a JAX-WS-specific type without the JAX-WS service factory bean. I have @Produces({ "application/json" } @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) before the class definition. I think Response can't be converted to JSON type. How can I convert and make it work ? – kss Mar 24 '15 at 08:28