微信小程序显示流格式照片

1.服务端,java代码,用于将图片转为文件流返回给前端

java 复制代码
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Demo {

    @GetMapping("/aaaaaa")
    public ResponseEntity<Resource> getImage(String imageName) throws Exception {
        // 构建图片的路径
        Path imagePath = Paths.get("D:\\1.png");
        
        // 检查文件是否存在
        if (!Files.exists(imagePath)) {
            throw new Exception("Image not found");
        }
        
        // 将图片文件转换为Resource对象,以便Spring管理
        Resource resource = new UrlResource(imagePath.toUri());
        
        // 设置响应头,指定内容类型和文件名(对于下载特别有用)
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + imageName);
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE); // 根据实际情况调整MIME类型
        
        // 返回文件流和响应头
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}

2.微信小程序端,请求文件流,并将图片转码

javascript 复制代码
getImage(){
	const that = this
	uni.request({
		url:"http://127.0.0.1/aaaaaa",
		method: 'GET',
		dataType:'json',
		responseType: 'arraybuffer', // 指定返回类型为二进制数据流
		data: {},
		success:(res)=>{
			// 将返回的文件流数据写入临时文件
			const fsm = wx.getFileSystemManager();
			const filePath = wx.env.USER_DATA_PATH + '/temp-image.jpg';
			fsm.writeFile({
			  filePath: filePath,
			  data: res.data,
			  encoding: 'binary',
			  success() {
				// 将临时文件路径绑定到页面上的image组件
				that.imgSrc=filePath;
			  }
			});
		},
		
	})
}
相关推荐
ThinkerQAQ_3 分钟前
并发编程(七):volatile——从语言规则到 CPU
java·python·go
蜗牛互联网7 分钟前
Python Responses API函数调用实战:工具白名单、参数校验与预算
java·人工智能·后端
韩曙亮10 分钟前
【JavaEE】MyBatis 基本用法 ⑤ ( 单条件查询 | 多条件查询 | 多条件动态查询 | 单条件动态查询 )
java·sql·java-ee·mybatis·mapper·sql映射
开开心心就好10 分钟前
二维码批量生成导出工具,离线可用完全免费
java·前端·人工智能·智能手机·github·excel·visual studio
wangbing112516 分钟前
开发指南147-WebSocket-前后关联关系
java
Wx-bishekaifayuan28 分钟前
springboot陨石鉴收系统96265-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spring·课程设计
code_slave(码畜)1 小时前
微服务架构落地:基础服务 —— 报表服务(上篇:定位、边界与整体架构)
java·spring boot·spring cloud·微服务·架构
不灭的黄金瞳1231 小时前
Java实现简易图书管理系统
java·intellij-idea
重生之小比特1 小时前
【C++进阶】02 Linux基本指令
java·数据库·c++
Wang's Blog1 小时前
Java 项目实战: 外卖平台优化-Nginx常用命令与环境变量配置
java·网络·nginx