如何在spring mvc rest controller中返回二进制数据而不是base64编码的byte []

时间:2016-01-08 11:30:21

标签: java spring spring-mvc

我想通过spring-mvc-rest控制器返回生成的pdf文件。这是我目前正在使用的代码的缩短版本:

@RestController
@RequestMapping("/x")
public class XController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<byte[]> find() throws IOException {
        byte[] pdf = createPdf();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "pdf"));
        headers.setContentDispositionFormData("attachment", "x.pdf");
        headers.setContentLength(pdf.length);
        return new ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
    }
}

这个工作得很好,它只是将实际的字节数组返回为base64编码:(

curl -i 'http://127.0.0.1:8080/app/x'

Server: Apache-Coyote/1.1
Content-Disposition: form-data; name="attachment"; filename=x.pdf"
Content-Type: application/pdf
Content-Length: 138654
Date: Fri, 08 Jan 2016 11:25:38 GMT

"JVBERi0xLjYNJeLjz9MNCjMyNCAwIG9iag [...]

(顺便说一下,答案甚至不包含结束" :)

任何提示都赞赏!

5 个答案:

答案 0 :(得分:4)

问题是由Spring尝试将响应编码为Json引起的。

您的请求可能指定Accepts = "*/*",因为Spring忽略了ResponseEntity的ContentType,所以最好的编码是application/json

最简单的解决方法是在您的请求映射中添加produces,以便您的代码变为:

@RestController
@RequestMapping(value = "/x",
                produces = "application/pdf") // <-- Add this
public class XController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<byte[]> find() throws IOException {
        byte[] pdf = createPdf();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "pdf"));
        headers.setContentDispositionFormData("attachment", "x.pdf");
        headers.setContentLength(pdf.length);
        return new ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
    }
}

答案 1 :(得分:2)

我使用你的代码创建了这个例子,但是一个非常类似的方法是在我的web应用程序中完成他的工作:

@RequestMapping(value = "/", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response,
                         HttpServletRequest request) throws IOException
{
    byte[] pdf = createPdf();

    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename=foo.pdf");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.getOutputStream().write(pdf);
}

否则,您可以尝试这个答案Open ResponseEntity PDF in new browser tab

答案 2 :(得分:1)

这是我的代码,工作正常,也许这可以帮到你。

@RequestMapping(value = "/createReport", method = RequestMethod.POST,produces="application/pdf")
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<byte[]> createReport(@RequestBody ReporteDTO reporteDTO) {
        byte[] outputReport = null;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/pdf"));
        headers.setContentDispositionFormData("inline", "archivo.pdf");
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
             outputReport = getFilePdf();
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(outputReport, headers, HttpStatus.OK);
        return response;
    } 

答案 3 :(得分:0)

将产生的属性添加到RequestMapping

@RequestMapping(path = "/download", produces = "application/pdf")

答案 4 :(得分:0)

距离answerRasmus Faber非常近。

就我而言:我想查看一个由 spring 生成的 PDF。我不想下载。
但是我每次都得到 base64 编码的字符串,而不是我的 byte[]

我发现我只能在 Firefox 中获得 base64 字符串。所以我改用了 chrome,Rasmus 的回答在那里工作正常。
现在,它也适用于 Firefox。但我不能说为什么(我什么都没改变)。我猜有些东西被缓存了。

有关详细信息,这可能有助于Incorrect content type for PDF file with Firefox

我使用流的工作代码:

@GetMapping(path = "/demo/pdf", produces = "application/pdf")
public StreamingResponseBody getPdf(final HttpServletResponse response)  {
    response.setContentType("application/pdf");
    response.setHeader("cache-control","must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Content-Disposition", "inline; filename=aCleverFileName.pdf");
    return outputStream -> this.pdfService.createPdf(outputStream);
}