1

I am developing a rest end point to download a existing pdf file. I'm able to download PDF with size, but when I open, getting error as

'Adobe reader couldnot open it is either not supported file type or because file has been damaged'.

I have noticed that in postman, response header content type is application/pdf;charset=UTF-8. I'm not sure if this is the cause.

With simple spring boot application I'm able to download, but in our project when I implement the same code, it is not working.

@GetMapping( path= "/s3/downloads")

public ResponseEntity<byte[]> downloadFile() 
  {
    InputStream in = getClass().getResourceAsStream("/com/consulting/cloud/filetransfers/rest/template_attendance_en_green_full.pdf");

byte[] generatedCertificate = null;
String downloadFileName = "pdfFile1.pdf";
ResponseEntity<byte[]> responseEntity = ResponseEntity.status( HttpStatus.NO_CONTENT ).body( generatedCertificate ); 
try {
  generatedCertificate =  StreamUtils.copyToByteArray(in);
  HttpHeaders headers = new HttpHeaders();
  headers.add("Access-Control-Allow-Origin", "*");
  headers.setContentType( MediaType.APPLICATION_PDF );
  headers.setContentLength( generatedCertificate.length );
  headers.setContentDispositionFormData( "attachment", downloadFileName );
  responseEntity = ResponseEntity.ok().headers( headers ).body( generatedCertificate );
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
return responseEntity;
}

The file is opened as expected.

Theo
  • 57,719
  • 8
  • 24
  • 41
vrk
  • 11
  • 3

0 Answers0