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


    服务端代码:

    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 的前缀路径,以适应你的图片存储路径。

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

  • 相关阅读:
    k8s命令行web代理神器gotty
    Spring Boot 文件上传 报错:FileNotFoundException Spring 异步文件上传 FileNotFoundException
    OpenShift 4 - 在 OpenShift 上安装 GitLab
    掌握Python爬虫实现网站关键词扩展提升曝光率
    13.MongoDB聚合管道
    数据库——表结构相关SQL
    报错——warning: ignoring JAVA_HOME=/home/jdk/jdk1.8.0_281; using bundled JDK
    python实战故障诊断之CWRU数据集(一):数据集初识
    JavaScript事件监听器总结
    产品评论观点提取Baseline-2021 CCF BDCI 数据挖掘 top3方案分享 数据+代码
  • 原文地址:https://blog.csdn.net/threadroc/article/details/139755004