S3 download pdf - REST API

依然范特西╮ 提交于 2019-11-29 11:54:01

Following simple code should work for you, not sure why are you trying to convert characters to bytes and vice-versa? Try this one, it works fine. Both PostMan/Browser.

@GET
@RequestMapping("/document")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces("application/pdf")
public ResponseEntity<InputStreamResource> getDocument() throws IOException {

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();

    S3Object object = s3.getObject("BUCKET-NAME", "DOCUMENT-URL");
    S3ObjectInputStream s3is = object.getObjectContent();


    return ResponseEntity.ok().contentType(org.springframework.http.MediaType.APPLICATION_PDF).cacheControl(CacheControl.noCache())
            .header("Content-Disposition", "attachment; filename=" + "testing.pdf")
            .body(new InputStreamResource(s3is));

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!