使用spring中restTemplate的api发送请求下载图片

       String url = "https://img3.doubanio.com/view/status/l/public/103861781-665301078817f01.jpg";
       //创建restTemplate对象
        org.springframework.web.client.RestTemplate restTemplate = new org.springframework.web.client.RestTemplate();
        /**
         * 请求地址
         * 请求方式
         * 请求实体类
         * 请求返回类型
         */
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class);
        //获取entity中的数据
        byte[] body = responseEntity.getBody();
        //创建输出流  输出到本地
        FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\fuwuqi\\1.jpg"));
        fileOutputStream.write(body);
        //关闭流
        fileOutputStream.close();

 

posted @ 2020-11-11 14:09  奶牛花花  阅读(5523)  评论(0编辑  收藏  举报