Spring MVC + Springfox 2.9.2 下载的 PDF 文件已损坏。

huangapple go评论53阅读模式
英文:

Spring MVC + Springfox 2.9.2 downloads damaged PDF

问题

以下是您要翻译的内容:

I have a Spring MVC project (no SpringBoot) with a GET endpoint which returns a PDF file. The PDF file is either generated manually or read from resources. I also have a SpringFox dependency to generate swagger-ui.html.

Dependency versions:

  • Spring: 4.3.25.RELEASE
  • SpringFox: 2.9.2

The problem is that when I try to download the PDF directly using the "Download file" button then the file is downloaded but somehow corrupted and impossible to open. But when I use the "Request URL" I'm able to download the PDF without any problem.

My REST request:

@RequestMapping(value = "/v1/generatePdfSync", method = RequestMethod.GET)
public ResponseEntity<Resource> generatePdfSync(@RequestParam String templateName) {
    Map<String, Object> model = new HashMap<>();
    model.put("title", "Hello world!");
    model put("pages", new ArrayList<>(Arrays.asList(1, 2, 3)));

    byte[] bytes = pdfGenerator.generatePdf(templateName, model);

    // Create response
    ByteArrayResource resource = new ByteArrayResource(bytes);
    return ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .contentLength(resource.contentLength())
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"test.pdf\"")
            .body(resource);
}

My question is whether I don't have some mistake in the request itself.

英文:

I have a Spring MVC project (no SpringBoot) with a GET endpoint which returns a PDF file. The PDF file is either generated manually or read from resources. I also have a SpringFox dependency to generate swagger-ui.html.

Dependency versions:

  • Spring: 4.3.25.RELEASE
  • SpringFox: 2.9.2

The problem is that when I try to download the PDF directly using the "Download file" button then the file is downloaded but somehow corrupted and impossible to open. But when I use the "Request URL" I'm able to download the PDF without any problem.
Spring MVC + Springfox 2.9.2 下载的 PDF 文件已损坏。

My REST request:

    @RequestMapping(value = &quot;/v1/generatePdfSync&quot;, method = RequestMethod.GET)
    public ResponseEntity&lt;Resource&gt; generatePdfSync(@RequestParam String templateName) {
        Map&lt;String, Object&gt; model = new HashMap&lt;&gt;();
        model.put(&quot;title&quot;, &quot;Hello world!&quot;);
        model.put(&quot;pages&quot;, new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3)));

        byte[] bytes = pdfGenerator.generatePdf(templateName, model);

        // Create response
        ByteArrayResource resource = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(resource.contentLength())
                .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;test.pdf\&quot;&quot;)
                .body(resource);
    }

My question is whether I don't have some mistake in the request itself.

答案1

得分: 0

抱歉,我无法识别代码中的 HTML 转义字符 &quot;,请提供原始的中文文本,我会尽力提供翻译。

英文:

The error was mine, this code worked for me:

@RequestMapping(value = &quot;/v2/generatePdfSync&quot;, method = RequestMethod.POST, produces = {MediaType.APPLICATION_PDF_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity&lt;Resource&gt; generatePdfSync(@RequestBody GeneratePdfRequest generatePdfRequest) {
//        Map&lt;String, Object&gt; model = new HashMap&lt;&gt;();
//        model.put(&quot;title&quot;, &quot;Hello world!&quot;);
//        model.put(&quot;pages&quot;, new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3)));

        LOGGER.info(&quot;GeneratePdfSync: {}&quot;, generatePdfRequest);

        byte[] bytes = pdfGenerator.generatePdf(generatePdfRequest.getTemplateName(), generatePdfRequest.getTemplateModel());

        // Create response
        ByteArrayResource resource = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(resource.contentLength())
                .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;&quot; + generatePdfRequest.getResultPdfName() + &quot;\&quot;&quot;)
                .body(resource);
    }

huangapple
  • 本文由 发表于 2023年2月14日 20:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定