当前位置: 首页 > 知识库问答 >
问题:

products=“application/pdf”不适用于Spring Boot

冯德佑
2023-03-14

我有一个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

在我的Rest控制器上,我添加到RequestMap产生="应用程序/pdf",但它不起作用。

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

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

}

我的准备响应方法:

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

共有2个答案

滕夜洛
2023-03-14

只是为了启动这个线程,如果有人在收到错误时到达这里,即找不到适合Application/pdf的转换器。
检查您是否使用@RepositoryRestController注释。
如果是这种情况,请尝试切换到@RestController

王兴庆
2023-03-14

添加代码产生={"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);
}
 类似资料:
  • 我编写了以下代码来禁用ChromePDF查看器,以便在Chrome打开链接时,PDF文件可以在C:\下载文件夹中自动下载。 不幸的是,我相信PDF查看器没有被正确禁用。以下是我打开PDF url时使用此代码得到的结果: 有没有其他解决方案可以在Chrome中自动下载文件?

  • 问题内容: 由于GAE具有严格的限制,例如-“ Java应用程序无法使用任何用于写入文件系统的类”。 是否有一个好的Java PDF库可以将PDF写入内存以流式传输到云? 问题答案: 根据这个在Google网上论坛上的主题(需要身份验证),PDFjet可以在GAE上使用(已进行了少许修改,以在某些地方用流替换文件)。正如他们在线程中所说: 这是一个相当低级的库,但是对于简单的任务应该可以。 到目前

  • 问题内容: 嗨,我只是简单地尝试在www.example.com上获取h1标签,该标签显示为“ Example Domain”。该代码适用于http://www.example.com,但不适用于https://www.exmaple.com。我该如何解决这个问题?谢谢 问题答案: PhantomJSDriver不支持(所有)DesiredCapabilities。 你会需要: 记录在这里:htt

  • 所以我使用这种方法写入文件,它在windows上运行完全正常,但在mac上运行时,它会创建文件,但它们是空的。 我知道数据是正确的,因为它打印正确。感谢您的任何帮助,这真的让我绊倒了。

  • 列名称的类型为int[] 上述查询适用于postgresql,但不适用于hsqldb,甚至适用于sql 尝试的hsqldb版本:2.2.9和2.3.0 在hsqldb中工作的sql是从table_name中选择x,unnest(column_name)y(x)x和y不是该表的列。