产生=" application / pdf"没有在Spring Boot上工作

时间:2016-07-10 16:45:21

标签: java spring file spring-boot

我有Spring Boot项目,我正在使用Jasper Report。我发布了一些json数据并回复给我:

%PDF-1.5
%����
1 0 obj
<undefined</Filter/FlateDecode/Length 29>>stream
x�+�r
�26S�00SI�r
�
��13-
endstream
endobj
3 0 obj<undefined</Tabs/S/Group<undefined</S/Transparency/Type/Group/CS/DeviceRGB>>/Contents 1 0 R/Type/Page/Resources<undefined</ColorSpace<</CS/DeviceRGB>>/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]>>/Parent 2 0 R/MediaBox[0 0 595 842]>>
endobj
2 0 obj
<undefined</Kids[3 0 R]/Type/Pages/Count 1/ITXT(2.1.7)>>
endobj
4 0 obj<undefined</Type/Catalog/Pages 2 0 R/ViewerPreferences<undefined</PrintScaling/AppDefault>>>>
endobj
5 0 obj<undefined</ModDate(D:20160710203902+05'00')/Creator(JasperReports Library version 6.2.0)/CreationDate(D:20160710203902+05'00')/Producer(iText 2.1.7 by 1T3XT)>>
endobj
xref
0 6
0000000000 65535 f 
0000000015 00000 n 
0000000333 00000 n 
0000000110 00000 n 
0000000396 00000 n 
0000000487 00000 n 
trailer<undefined</Info 5 0 R/ID [undefined<07942c7c1b5b6068753ddc445ec60abf>undefined<c82bba08c068c3699915ac33668fef92>]/Root 4 0 R/Size 6>>
startxref
654
%%EOF

在我的休息控制器上,我添加到RequestMapping产生=&#34; application / pdf&#34;但它没有用。

@RequestMapping(value = "/gMapReports", method = RequestMethod.POST, produces = "application/pdf")
public ResponseEntity<InputStreamResource> gMapReports(@RequestBody GMapReportRequest gMapReportRequest) {

    return reportService.prepareResponse(reportService.gMapReports(gMapReportRequest));

}

我的prepareResponse方法:

public ResponseEntity<InputStreamResource> prepareResponse(File reportDocument) {
    FileInputStream fileInputStream = fileStreamConverter.getFileInputStream(reportDocument);

    return ResponseEntity
            .ok()
            .contentLength(reportDocument.length())
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .header("content-disposition", "filename=report")
            .body(new InputStreamResource(fileInputStream));
}

1 个答案:

答案 0 :(得分:1)

添加代码produces = {"application/pdf"}对我有用。完整示例:

@RequestMapping(value = "/pdfFile", method = RequestMethod.POST, produces = {"application/pdf"})
@ResponseBody
public FileSystemResource getFile(@ModelAttribute PdfFile pdfFile) {
    PdfFileGenerator pdfFileGenerator = new PdfFileGeneratorImpl();
    File file = pdfFileGenerator.generatePdf(pdfFile);
    return new FileSystemResource(file);
}