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


    服务端代码:

    1. import org.springframework.core.io.InputStreamResource;
    2. import org.springframework.http.HttpHeaders;
    3. import org.springframework.http.HttpStatus;
    4. import org.springframework.http.ResponseEntity;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RequestParam;
    8. import org.springframework.web.client.RestTemplate;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. @Controller
    12. @RequestMapping("/api")
    13. public class ImageController {
    14. @Autowired
    15. private RestTemplate restTemplate;
    16. @GetMapping("/get-image")
    17. public ResponseEntity getImage(@RequestParam String identifier, @RequestParam String path) {
    18. // 构建客户端请求URL
    19. String clientUrl = "http://client-server/api/fetch-image?identifier=" + identifier + "&path=" + path;
    20. // 通过RestTemplate调用客户端接口
    21. ResponseEntity<byte[]> response = restTemplate.getForEntity(clientUrl, byte[].class);
    22. if (response.getStatusCode() == HttpStatus.OK) {
    23. // 将客户端返回的图片数据封装到InputStreamResource中
    24. InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(response.getBody()));
    25. // 设置响应头
    26. HttpHeaders headers = new HttpHeaders();
    27. headers.add("Content-Type", "image/png");
    28. return new ResponseEntity<>(resource, headers, HttpStatus.OK);
    29. } else {
    30. return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    31. }
    32. }
    33. }

    客户端代码:

    1. import org.springframework.core.io.InputStreamResource;
    2. import org.springframework.http.HttpHeaders;
    3. import org.springframework.http.HttpStatus;
    4. import org.springframework.http.ResponseEntity;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RequestParam;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import java.io.FileInputStream;
    10. import java.io.IOException;
    11. import java.io.InputStream;
    12. @Controller
    13. @RequestMapping("/api")
    14. public class ClientImageController {
    15. @GetMapping("/fetch-image")
    16. public ResponseEntity<byte[]> fetchImage(@RequestParam String identifier, @RequestParam String path) {
    17. // 构建图片文件的完整路径
    18. String fullPath = "/path/to/images/" + identifier + path;
    19. try {
    20. // 打开图片文件的输入流
    21. InputStream inputStream = new FileInputStream(fullPath);
    22. // 读取图片数据
    23. byte[] imageData = inputStream.readAllBytes();
    24. inputStream.close();
    25. // 设置响应头
    26. HttpHeaders headers = new HttpHeaders();
    27. headers.add("Content-Type", "image/png");
    28. return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
    29. } catch (IOException e) {
    30. // 处理文件未找到或其他IO异常
    31. return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    32. }
    33. }
    34. }

    说明

    1. 服务端

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

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

    配置

    1. RestTemplate Bean(在服务端的配置类中添加):
    1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.web.client.RestTemplate;
    4. @Configuration
    5. public class AppConfig {
    6. @Bean
    7. public RestTemplate restTemplate() {
    8. return new RestTemplate();
    9. }
    10. }

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

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

  • 相关阅读:
    使用go-redis/redis依赖操作redis
    【TFS-CLUB社区 第7期赠书活动】〖从零开始利用Excel与Python进行数据分析 自动化办公实战宝典〗等你来拿,参与评论,即可有机获得
    如何确保PFMEA中的控制措施能够得到有效执行?
    1.1信息系统与信息化-1.2信息系统开发方法
    力扣146|LRU缓存淘汰算法
    一本通2059;买笔
    高效数据湖构建与数据仓库融合:大规模数据架构最佳实践
    汽车螺丝扭力标准/汽车常见螺栓扭矩参照
    linux之tcpdump的用法
    极限导论总结
  • 原文地址:https://blog.csdn.net/threadroc/article/details/139755004