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

 

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