使用restTemplate的get方法调用其他系统接口下载文件

使用restTemplate的get方法调用其他系统接口下载文件

被调用的接口

//从win10或者tomcat上下载文件 
@RequestMapping(value = "/download", method = {RequestMethod.GET})
    @PassToken
    public String downloadFile(HttpServletRequest request, HttpServletResponse response,String id,String name) throws UnsupportedEncodingException {
            String  information="";
            String fileName = name;
            String s = tmodeluseMapper.downloadDocument(name,id );
            if (fileName != null) {
                //设置文件路径
                File file = new File("E:\\apache-tomcat-8.5.5\\"+s);
                if (file.exists()) {
                    // 设置强制下载不打开
                    response.setContentType("application/force-download");
                    // 设置文件名
                    response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"ISO8859-1"));
                    byte[] buffer = new byte[1024];
                    FileInputStream fis = null;
                    BufferedInputStream bis = null;
                    try {
                        fis = new FileInputStream(file);
                        bis = new BufferedInputStream(fis);
                        OutputStream os = response.getOutputStream();
                        int i = bis.read(buffer);
                        while (i != -1) {
                            os.write(buffer, 0, i);
                            i = bis.read(buffer);
                        }
                        information= "下载成功!";
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (bis != null) {
                            try {
                                bis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (fis != null) {
                            try {
                                fis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            return "下载失败";
        }

调用其他接口 并且下载文件

 @RequestMapping(value = "/test2", method = {RequestMethod.GET})
    public void setRestTemplate(HttpServletRequest httpServletRequest,HttpServletResponse response,String id,String name) throws IOException  {
           String url = "http://localhost:8080/tcartransfer/download?id="+id+"&name="+name;
            HttpHeaders headers = new HttpHeaders();
            headers.add("token",httpServletRequest.getHeader("token"));
            headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
            HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<byte[]> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);

        try {
            response.setHeader("Content-disposition", "attachment;filename=text" + name);
            //getOutputStream引擎创建的字节输出流对象
            response.getOutputStream().write(exchange.getBody());
        } catch (IOException e) {
            System.out.println("下载失败");
        }

    }

调用其他接口 保存在本机

 @RequestMapping(value = "/test2", method = {RequestMethod.GET})
    public void setRestTemplate(HttpServletRequest httpServletRequest) throws IOException  {
           String url = "http://localhost:8080/tcartransfer/download?id=00000177-3e1a-5bd6-54e9-346afd154084&name=1606893694(1).png";
            HttpHeaders headers = new HttpHeaders();
            headers.add("token",httpServletRequest.getHeader("token"));
            headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
            HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<byte[]> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);

        
        System.out.println("文件下载请求结果状态码:" + exchange.getStatusCode());
        // 将下载下来的文件内容保存到本地
        String targetPath = "C:\\Users\\Administrator\\Desktop\\qywx\\splash-down.png";
        Files.write(Paths.get(targetPath), Objects.requireNonNull(exchange.getBody(),
                "未获取到下载文件"));

    }
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值