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

服务端代码:

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

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

相关推荐
2501_915918418 小时前
Web 前端可视化开发工具对比 低代码平台、可视化搭建工具、前端可视化编辑器与在线可视化开发环境的实战分析
前端·低代码·ios·小程序·uni-app·编辑器·iphone
2501_915106328 小时前
iOS 使用记录和能耗监控实战,如何查看电池电量消耗、App 使用时长与性能数据(uni-app 开发调试必备指南)
android·ios·小程序·uni-app·cocoa·iphone·webview
じòぴé南冸じょうげん16 小时前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
2501_9160137417 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184119 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张20 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
微三云-轩20 小时前
区块链:重构企业数字化的信任核心与创新动力
人工智能·小程序·区块链·生活·我店
2501_915918412 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520862 天前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app
一口十个小甜虾2 天前
微信小程序体验版,当打开调试模式正常访问,关闭之后无法访问
微信小程序·小程序