使用HttpClient從服務器獲取圖片再交給前端展示

public static void getImage(HttpServletRequest request, HttpServletResponse response){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("請求地址");
        CloseableHttpResponse resp = null;
        try {
            resp = httpClient.execute(httpPost);// 調用服務器接口
            byte[] data = EntityUtils.toByteArray(resp.getEntity());// 將圖片或者文件轉化成數組的形式
            OutputStream toClient = response.getOutputStream();// 獲取輸出流
            toClient.write(data);// 將這個數組寫入輸出流中
            toClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章