How to return binary data instead of base64 encoded byte [] in spring mvc break controller

I want to return the generated PDF file through the spring -mvc-rest controller. This is a shortened version of the code I'm currently using:

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

This works almost fine, it just returns the actual base64 encoded byte array :(

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 [...]

(By the way, the answer doesn’t even contain a closure ":)

Any hints appreciated!

+4
source share
3 answers

I created an example using your code, but a very similar method does its job in my web application:

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

Otherwise, you can try this answer Open ResponseEntity PDF in a new browser tab

+1

, 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);
    }
}
+3

, , .

@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;
    } 
+1
source

Source: https://habr.com/ru/post/1623412/


All Articles