spring-boot - 如何使用 Postman 从 Spring REST 服务下载 PDF

标签 spring-boot postman

我有一个基于 Spring 的 Rest 服务,它提供 PDF 作为响应。使用下面的代码,我可以在 postman 中以二进制值形式获取 PDF 内容。我的问题是当我调用该服务时将其作为附件下载。

要实现此目的,我需要对代码或客户端进行任何更改吗?

@GetMapping(value="/getUserpdf")
    public ResponseEntity<Resource> getUserInfo(@RequestHeader(name="reqHeader") Map<String, String> reqHeader,
                                                  @RequestParam(name="userId",required=true) String userId){

        MetaInfo metaInfo = getHeaderValues(reqHeader);

        //To get Actual PDF content as Bytes
        byte[] pdfBytes = getUserPdfService.getUserInfo(metaInfo,userId);

        ByteArrayResource resource = new ByteArrayResource(pdfBytes);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=UserInfo.pdf");



        return ResponseEntity
                      .ok()
                      .headers(headers)
                      .contentLength(pdfBytes.length)
                      .contentType(MediaType.parseMediaType("application/octet-stream")).body(resource);
    }

我还注册了我的转换器

@Bean
    public HttpMessageConverters customConverters() {
        ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        return new HttpMessageConverters(arrayHttpMessageConverter);
    }

最佳答案

这是一个例子:

@GetMapping("/getUserpdf/{id}")
    @CrossOrigin
    @ResponseBody
    public ResponseEntity<InputStreamResource> downloadFile(@PathVariable(required = true, value = "id") Long id,@RequestParam(name="userId",required=true) String userId,HttpServletRequest request) throws IOException {

        //To get Actual PDF content as Bytes
        byte[] pdfBytes = getUserPdfService.getUserInfo(id,userId);
        if (Objects.nonNull(pdfBytes)) {
            String fileName = "UserInfo.pdf";
            MediaType mediaType = MediaType.parseMediaType("application/pdf");
            File file = new File(fileName);
            FileUtils.writeByteArrayToFile(file, pdfBytes); //org.apache.commons.io.FileUtils
            InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

            return ResponseEntity.ok()
                    // Content-Disposition
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
                    // Content-Type
                    .contentType(mediaType)
                    // Contet-Length
                    .contentLength(file.length()) //
                    .body(resource);
        } else {
            throw ResponseEntity.notFound().build();
        }
    }

注意:我不确定媒体类型,但您可以确认是否可以!

关于spring-boot - 如何使用 Postman 从 Spring REST 服务下载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767884/

相关文章:

api - postman 上的授权类型不记名 token 和来自 API 的请求数据,这需要带有 flutter 的 token 不记名

perl - Postman Oauth 1.0 签名与我在 Perl 中得到的签名不匹配

spring - 从 Spring Boot 1.3.0M1 切换到 1.3.0M2 时出现 "NoClassDefFoundError: GenericApplicationListener"

java - 使用 Spring Security Version 5 和 Feign Client(最新版本)时如何处理漏洞 CVE-2018-1258?

botframework - 400 来自使用 Postman 的本地机器人的错误请求

javascript - 使用 postman 集合中的 Newman 脚本输出responseBody

java - Spring 启动和 tomcat

java - Spring Boot - org.springframework.beans.factory.BeanCreationException : Error creating bean with name

java - 处理异常时保存不起作用

curl - 将 curl 请求从 Chrome 复制到 Postman 无法按预期工作