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

若依框架,小程序访问后端,后端访问客户端,客户端读取图片返回

服务端代码:

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;@Controller
@RequestMapping("/api")
public class ImageController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/get-image")public ResponseEntity<InputStreamResource> getImage(@RequestParam String identifier, @RequestParam String path) {// 构建客户端请求URLString clientUrl = "http://client-server/api/fetch-image?identifier=" + identifier + "&path=" + path;// 通过RestTemplate调用客户端接口ResponseEntity<byte[]> response = restTemplate.getForEntity(clientUrl, byte[].class);if (response.getStatusCode() == HttpStatus.OK) {// 将客户端返回的图片数据封装到InputStreamResource中InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(response.getBody()));// 设置响应头HttpHeaders headers = new HttpHeaders();headers.add("Content-Type", "image/png");return new ResponseEntity<>(resource, headers, HttpStatus.OK);} else {return new ResponseEntity<>(HttpStatus.NOT_FOUND);}}
}

客户端代码:

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;@Controller
@RequestMapping("/api")
public class ClientImageController {@GetMapping("/fetch-image")public ResponseEntity<byte[]> fetchImage(@RequestParam String identifier, @RequestParam String path) {// 构建图片文件的完整路径String fullPath = "/path/to/images/" + identifier + path;try {// 打开图片文件的输入流InputStream inputStream = new FileInputStream(fullPath);// 读取图片数据byte[] imageData = inputStream.readAllBytes();inputStream.close();// 设置响应头HttpHeaders headers = new HttpHeaders();headers.add("Content-Type", "image/png");return new ResponseEntity<>(imageData, headers, HttpStatus.OK);} catch (IOException e) {// 处理文件未找到或其他IO异常return new ResponseEntity<>(HttpStatus.NOT_FOUND);}}
}

说明

  1. 服务端

    • 使用 RestTemplate 调用客户端的接口。
    • 将客户端返回的图片数据封装到 InputStreamResource 中,并返回给前端。
  2. 客户端

    • 接收服务端的请求,读取本地图片文件。
    • 将图片数据以字节数组的形式返回给服务端。

配置

  1. RestTemplate Bean(在服务端的配置类中添加):
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class AppConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}

2.图片路径:根据你的实际情况修改 fullPath 的前缀路径,以适应你的图片存储路径。

这个示例代码展示了如何在服务端调用客户端读取本地图片并返回给前端的基本流程。如果需要处理更多的业务逻辑或错误情况,可以进一步完善代码。

http://www.lryc.cn/news/382119.html

相关文章:

  • os7安装gitlab
  • 木头姐:将出于经济方面的考虑支持特朗普
  • sql注入登陆绕过
  • QT利用QGraphicsDropShadowEffect效果及自定义按钮来实现一个炫酷键盘
  • 机器学习(一)
  • 【深度学习】python之人工智能应用篇——图像生成技术(一)
  • java 非srping 使用r2dbc操作mysql 增删改查代码
  • 假冒国企现形记:股权变更视角下的甄别分析
  • Django 使用Apscheduler执行定时任务
  • Shopee API接口:获取搜索栏生成的商品结果列表
  • 选择门店收银系统要考虑哪些方面?美业系统Java源码分享私
  • 智慧养老的养老方式及其技术实现与趋势
  • 思维导图之计算机网络整体框架
  • P7771 【模板】欧拉路径
  • 卷积神经网络(CNN)理解
  • Databend 开源周报第 149 期
  • Hue Hadoop 图形化用户界面 BYD
  • 【经验分享】RT600 serial boot mode测试
  • 七种不同类型测宽仪技术参数 看看哪种能用于您的产线?
  • 【GO】rotatelogs库和sirupsen/logrus库实现日志功能的实践用例
  • Arc2Face - 一张图生成逼真的多风格人脸,本地一键整合包下载
  • swiper 幻灯片
  • Ubuntu 使用Vscode的一些技巧 ROS
  • JS中的三种事件模型
  • 南京邮电大学计算机网络实验二(网络路由器配置RIP协议)
  • 仓颉语言的编译和构建
  • 网络基础-协议
  • 电子设备抗震等级与电子设备震动实验
  • 你还在手动操作仓库?这款 CLI 工具让你效率飙升300%!
  • 未来已来!GPT-5震撼登场,工作与生活面临新变革!