java 调用第三方接口返回文件流并下载文件

@PostMapping("/file/download")
    public AjaxResult downFileById(@RequestBody FileInput input,HttpServletResponse response) throws IOException {
        String creditCode = input.getCreditCode();
        //生成 sso
        String sso = createSso(creditCode);
        Long fileId = input.getFileId();
        Map<String,Object> requestParam = new HashMap<>();
        // 设置请求参数
        requestParam.put("sso",sso);
        requestParam.put("id",fileId);
        // 调用第三方接口
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity request = new HttpEntity(requestParam, headers);
        //当接受一个文件流时必须使用 resource (spring)接收
        ResponseEntity<Resource> entity = restTemplate.postForEntity(FILEURL, request,Resource.class);
        InputStream in = entity.getBody().getInputStream();
        //调用下载文件接口
        FileUtils fileUtils = new FileUtils();
        fileUtils.downloadFile(response,in,"123.doc");
        return AjaxResult.success();
    }




public AjaxResult downloadFile(HttpServletResponse response, InputStream inputStream, String filename) {
        filename = UriUtils.encode(filename, "UTF-8");
        response.setContentType("multipart/form-data;");
        response.setContentType("application/force-download");
        //设置下载的文件名
        response.addHeader("Content-Disposition", "attachment;fileName*=UTF-8''" + filename);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(inputStream);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            return AjaxResult.error();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }

        return null;
    }

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用iText生成PDF文件需要以下几个步骤: 1. 引入iText库 在项目中引入iText库,可以通过Maven等构建工具导入依赖,也可以手动下载jar包导入。 2. 创建PDF文档 使用iText创建一个PDF文档对象,如下所示: ``` Document document = new Document(); ``` 3. 创建PDF输出 创建一个输出,将PDF文档内容输出到指定的文件或者网络中,如下所示: ``` PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); ``` 4. 打开文档 打开文档,开始编辑内容,如下所示: ``` document.open(); ``` 5. 编辑文档 编辑文档内容,可以插入文字、图片、表格等元素,也可以设置页面大小、边距等属性,具体使用方法见iText官方文档。 6. 关闭文档 编辑完成后,关闭文档,如下所示: ``` document.close(); ``` 完整代码示例: ``` Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); document.add(new Paragraph("Hello World!")); document.close(); ``` 根据调用第三方接口返回文件生成PDF文件,可以将文件读取到内存中,然后通过iText的Image类创建图片对象,将图片插入到PDF中,具体代码示例如下: ``` // 假设fileStream是调用第三方接口返回文件 InputStream fileStream = ...; // 创建PDF文档对象和PDF输出 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); // 打开文档,开始编辑内容 document.open(); // 创建图片对象 Image image = Image.getInstance(IOUtils.toByteArray(fileStream)); // 设置图片大小 image.scaleAbsolute(400, 400); // 插入图片到PDF中 document.add(image); // 关闭文档 document.close(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值