当前位置: 首页 > news >正文

get请求图片文件转为base64编码

    public static String convertImageToBase64(String url) throws IOException {String urls = url.replaceAll("192.168.10.242", "192.192.192.192");// 创建HTTP客户端try (CloseableHttpClient httpClient = HttpClients.createDefault();// 发送GET请求CloseableHttpResponse response = httpClient.execute(new HttpGet(urls));// 获取图片输入流InputStream inputStream = response.getEntity().getContent()) {// 验证响应状态if (response.getStatusLine().getStatusCode() != 200) {throw new IOException("获取图片失败,状态码: " + response.getStatusLine().getStatusCode());}// 读取图片字节ByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}// 获取图片MIME类型String contentType = response.getEntity().getContentType().getValue();// 转换为Base64并添加数据头return "data:" + contentType + ";base64," +Base64.getEncoder().encodeToString(outputStream.toByteArray());}}public static void main(String[] args) throws IOException {System.out.println(convertImageToBase64("http://192.168.14.4:8803/1.png"));}

 

http://www.hskmm.com/?act=detail&tid=8478

相关文章:

  • BMS与威纶通人机界面通信问题
  • Blazor全栈是个陷阱
  • 大型语言模型安全实践:Copilot安全防护经验总结
  • 一些编程语言的发展史
  • mysql生成uuid,3种实用方法详解
  • vmware ubuntu共享文件夹
  • 【10章】n8n+AI工作流:从入门到企业级AI应用实战
  • CodeGPT AI代码狂潮来袭!个人完全免费使用谷歌Gemini大模型 超越DeepSeek几乎是地表最强
  • svg和canvas的区别
  • 固态电池革命:我们离“续航焦虑终结者”还有多远?
  • 心得
  • Android 安卓 困难处理记录 腾讯IM和厂商离线推送难题 点击离线推送无法唤醒APP启动页但某些Service服务和Application被启动
  • 9.18
  • Codeforces Round 1051 (Div 2)
  • scheduleAtFixedRate
  • CRMEB标准版PHP核销功能深度解析,附权限配置技巧
  • 一文详细说明大模型安全评估要怎么做
  • apache doris 和 clickhouse的区别
  • Python numba jit加速计算
  • 人机协作开发新体验:花两天时间与Cursor共同打造一个微信小程序
  • OEC-Turbo刷群晖Armbian流程记录
  • 01_网络分层模型
  • SaaS 是什么?一文带你看懂 SaaS 与传统软件的区别
  • FreeCAD-即时入门-全-
  • UOS统信服务器操作系统V20(1070)安装mysql8.0.41(建议安装glibc2.28版本)
  • MyEMS:重新定义人与能源的关系 —— 一场藏在数据里的能源管理革命
  • 刀齿磨损智能检测APP
  • TJOI2007--线段
  • ceph集群的部署
  • 充电桩测试:守护绿色出行的安全密码