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

服务端代码:

java 复制代码
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 {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/get-image")
    public ResponseEntity<InputStreamResource> getImage(@RequestParam String identifier, @RequestParam String path) {
        // 构建客户端请求URL
        String 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);
        }
    }
}

客户端代码:

java 复制代码
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(在服务端的配置类中添加):
java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

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

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

相关推荐
老李不敲代码4 小时前
榕壹云预约咨询系统:基于ThinkPHP+MySQL+UniApp打造的灵活预约小程序解决方案
mysql·微信小程序·小程序·uni-app·php
fakaifa5 小时前
【最新版】西陆健身系统源码全开源+uniapp前端
前端·小程序·uni-app·开源·php·约课小程序·健身小程序
二J6 小时前
管理100个小程序-很难吗
android·小程序
qq_357389637 小时前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码,医护小程序
小程序
橘猫云计算机设计19 小时前
springboot-基于Web企业短信息发送系统(源码+lw+部署文档+讲解),源码可白嫖!
java·前端·数据库·spring boot·后端·小程序·毕业设计
跨时代科技1 天前
洗车小程序系统前端uniapp 后台thinkphp
小程序
fakaifa1 天前
【最新版】沃德代驾源码全开源+前端uniapp
前端·小程序·uni-app·开源·php·沃德代驾·代驾小程序
18538162800余。1 天前
NFC 碰一碰实现视频源码,网页与小程序协同
小程序
码起来呗2 天前
基于微信小程序的走失儿童帮助系统-项目分享
微信小程序·小程序
大樊子2 天前
微信小程序路由跳转实现详解
微信小程序·小程序