私は春に休憩APIを呼び出すpdfファイルをダウンロードする必要があります。私のコードは以下の通りです:iTextを使って安静時のサービスからスプリングをダウンロード
@RequestMapping(value = "downloadPDF", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
public @ResponseBody ResponseEntity<InputStreamResource> getPDF() throws IOException {
ByteArrayInputStream bis = GeneratePdfReport.createReport();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=migration.pdf");
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_PDF)
.body(new InputStreamResource(bis));
}
と、この文書に簡単な表を印刷しますCreateReport方法ですが、私は残りのAPIを呼び出すと、私はこのエラーを取得するときに問題が発生し
public static ByteArrayInputStream createReport() {
Document document = new Document();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(60);
table.setWidths(new int[]{1, 3, 3});
Font headFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
PdfPCell hcell;
hcell = new PdfPCell(new Phrase("Id", headFont));
hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hcell);
hcell = new PdfPCell(new Phrase("Name", headFont));
hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hcell);
hcell = new PdfPCell(new Phrase("Population", headFont));
hcell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hcell);
PdfWriter.getInstance(document, out);
document.open();
document.add(table);
document.close();
} catch (DocumentException ex) {
System.out.println("error");
}
return new ByteArrayInputStream(out.toByteArray());
}
サーバー
私はhttps://stackoverflow.com/questions/45159523/download-file-using-spring-restfulで解決-service – chris