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

2024-06-17 23:28

本文主要是介绍若依框架,小程序访问后端,后端访问客户端,客户端读取图片返回,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

服务端代码:

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.chinasem.cn/article/1070698

相关文章

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

一道经典Python程序样例带你飞速掌握Python的字典和列表

Python中的列表(list)和字典(dict)是两种常用的数据结构,它们在数据组织和存储方面有很大的不同。 列表(List) 列表是Python中的一种有序集合,可以随时添加和删除其中的元素。列表中的元素可以是任何数据类型,包括数字、字符串、其他列表等。列表使用方括号[]表示,元素之间用逗号,分隔。 定义和使用 # 定义一个列表 fruits = ['apple', 'banana

在服务器上浏览图片

@StarSky 2018-10-26 15:09 字数 15971 阅读 28 https://www.zybuluo.com/StarSky/note/1294871 来源 2018-09-27 线上服务器安装 imgcat Tool   2018-09-27 线上服务器安装 imgcat 0. 准备文件:iterm2_shell_integration.bash1. 在有权限

vue3项目将所有访问后端springboot的接口统一管理带跨域

vue3项目将所有访问后端springboot的接口统一管理带跨域 一、前言1.安装Axios2.创建Axios实例3.创建API服务文件4.在组件中使用API服务 二、跨域三、总结 一、前言 在Vue 3项目中,统一管理所有访问后端Spring Boot接口的最佳实践是创建一个专门的API服务层。这可以让你的代码更加模块化、可维护和集中管理。你可以使用Axios库作为HTT

【zabbix】zabbix客户端配置

1、部署zabbix客户端 #zabbix 5.0 版本采用 golang 语言开发的新版本客户端 agent2 。#zabbix 服务端 zabbix_server 默认使用 10051 端口,客户端 zabbix_agent2 默认使用 10050 端口。systemctl disable --now firewalldsetenforce 0hostnamectl set-host

局域网内vue2 配置本地IP地址访问项目

在日常开发中同事可能需要访问你的前端项目,可以通过配置实现通过ip访问 一.首先找到config文件夹目录下的 index.js文件             将此处的host的值修改为0.0.0.0(即 host: 0.0.0.0) // Various Dev Server settings//host: 'localhost' //将localhost进行替换成 0.0.0.0host:

美容美发店营销版微信小程序源码

打造线上生意新篇章 一、引言:微信小程序,开启美容美发行业新纪元 在数字化时代,微信小程序以其便捷、高效的特点,成为了美容美发行业营销的新宠。本文将带您深入了解美容美发营销微信小程序,探讨其独特优势及如何助力商家实现业务增长。 二、微信小程序:美容美发行业的得力助手 拓宽客源渠道:微信小程序基于微信社交平台,轻松实现线上线下融合,帮助商家快速吸引潜在客户,拓宽客源渠道。 提升用户体验:

BD错误集锦5——java.nio.file.FileSystemException 客户端没有所需的特权

问题:在运行storm本地模式程序时,java.nio.file.FileSystemException  客户端没有所需的特权   解决方式:以管理员身份运行IDEA即可。