这适用于Jetty 9
@GET
@Path("pdf/file")
@Produces("application/pdf")
public Response getFile() {
String fileName = Environment.getFilePath() + testFile;
File file = Environment.getFile(fileName);
Response response = Response.ok((Object) file).build();
return response;
}
这个
@GET
@Path("pdf/plain")
@Produces("application/pdf")
public Response getPlainPDF() {
String formName = Environment.getFilePath() + testFile;
PDDocument pdfDoc = PDF.getPDFDocument(formName);
PDStream ps = new PDStream(pdfDoc);
try {
OutputStream outputStream = ps.createOutputStream();
Response response = Response.ok((Object) outputStream).build();
return response;
} catch (IOException e) {
System.out.println("Error creating PDF response " + e.getMessage());
return null;
}
}
失败,“找不到媒体类型= application / pdf的MessageBodyWriter”。是否可以使用PDFBox以编程方式创建PDF并将其作为PDF文件提供,而不是先将其保存到磁盘上?