使用JAXWS / cxf服务发送pdf / doc / txt / image文件Spring

时间:2015-03-17 12:24:19

标签: spring pdf cxf jax-ws

有人可以向我提供发送pdf文件作为回复的演示吗?

端点是

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

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

DAO将是

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简化了发送方式。有人可以详细说明使用MTOM吗?

1 个答案:

答案 0 :(得分:1)

您需要在响应中指定Content-Disposition标头并将文件写入响应实体。所以对于例如:

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();
}

如何将文件转换为byte []可以找到here