You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot实现Excel文件MongoDB二进制存储与读取报错求助

解决方案:SpringBoot 存储/读取 MongoDB Binary 格式 Excel 文件

问题根源

你碰到的"stream not supported"错误,核心原因是读取Binary数据时流操作方式不对——直接转字节数组存Binary本身没问题,但读取时如果误用了MongoDB内部未开放的流接口,就会触发这个报错。

正确实现步骤

1. 定义MongoDB实体类

实体类里的文件字段直接用org.bson.types.Binary类型,别自己封装字节数组:

import org.bson.types.Binary;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;

@Document(collection = "excel_files")
@Data
public class ExcelFileEntity {
    private String id;
    private String fileName;
    // 存储Excel二进制数据
    private Binary fileData;
}

2. 上传存储Excel文件

处理MultipartFile上传时,直接拿文件字节数组构造Binary,不用手动关闭原始流(Spring会自动处理):

import org.bson.types.Binary;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

@Service
public class ExcelFileService {
    private final MongoTemplate mongoTemplate;

    public ExcelFileService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public String saveExcelFile(MultipartFile file) throws Exception {
        ExcelFileEntity entity = new ExcelFileEntity();
        entity.setFileName(file.getOriginalFilename());
        // 用文件字节数组直接构造Binary
        entity.setFileData(new Binary(file.getBytes()));
        mongoTemplate.save(entity);
        return entity.getId();
    }
}

3. 读取还原Excel文件

读取时从Binary里取出字节数组,用ByteArrayInputStream包装成流,别碰MongoDB的内部流:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.types.Binary;
import java.io.ByteArrayInputStream;

public XSSFWorkbook getExcelFile(String fileId) throws Exception {
    ExcelFileEntity entity = mongoTemplate.findById(fileId, ExcelFileEntity.class);
    if (entity == null) {
        throw new RuntimeException("文件不存在");
    }
    Binary fileData = entity.getFileData();
    // 从Binary获取字节数组,构造输入流
    ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData.getData());
    // 用流创建XSSFWorkbook
    return new XSSFWorkbook(inputStream);
}

4. 前端下载文件的处理

如果要把文件返回给前端下载,直接把字节数组写入响应流即可:

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

public void downloadExcel(String fileId, HttpServletResponse response) throws Exception {
    ExcelFileEntity entity = mongoTemplate.findById(fileId, ExcelFileEntity.class);
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename=" + entity.getFileName());
    OutputStream out = response.getOutputStream();
    out.write(entity.getFileData().getData());
    out.flush();
    out.close();
}

依赖版本验证

确保你的pom.xml里依赖了正确版本的库:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>

内容的提问来源于stack exchange,提问作者Lazy Techie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 20:24:31