使用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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM