【问题标题】:Sending pdf/doc/txt/image files with JAXWS/cxf services Spring使用 JAXWS/cxf 服务 Spring 发送 pdf/doc/txt/image 文件
【发布时间】:2015-03-17 12:24:18
【问题描述】:

谁能给我一个发送 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 吗?

【问题讨论】:

    标签: spring pdf cxf jax-ws


    【解决方案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();
    }
    

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

    【讨论】:

    • 您好,谢谢,但我遇到了错误。服务类 方法 pdfiles 部分 {http:///}PDFilesResponse 无法映射到架构。检查是否使用了没有 JAX-WS 服务工厂 bean 的特定于 JAX-WS 的类型。我在类定义之前有 @Produces({ "application/json" } @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)。我认为响应无法转换为 JSON 类型。如何转换并使其工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2021-06-15
    • 2015-04-09
    • 1970-01-01
    • 2014-07-16
    相关资源
    最近更新 更多