qw-110-

news/2025/10/31 2:37:07/文章来源:https://www.cnblogs.com/lihuadaiyu/p/19178488

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

public class ExcelUploadService {

private final WebClient webClient;

public ExcelUploadService(String thirdPartyBaseUrl) {
this.webClient = WebClient.builder()
.baseUrl(thirdPartyBaseUrl)
.build();
}

/**
* 上传Excel文件到第三方接口
* @param workbook Excel工作簿对象
* @param fileName 文件名
* @param additionalParams 额外的参数
* @param uploadUrl 上传接口URL
* @return 响应结果
*/
public Mono<String> uploadExcel(Workbook workbook, String fileName,
Map<String, String> additionalParams, String uploadUrl) {
try {
// 1. 将Workbook转换为字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
byte[] fileBytes = outputStream.toByteArray();
outputStream.close();

// 2. 构建Multipart表单数据
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();

// 添加Excel文件
bodyBuilder.part("file", fileBytes)
.filename(fileName)
.contentType(MediaType.APPLICATION_OCTET_STREAM);

// 添加其他参数
if (additionalParams != null) {
additionalParams.forEach((key, value) ->
bodyBuilder.part(key, value));
}

// 3. 构建请求体
var multipartBody = bodyBuilder.build();

// 4. 发送POST请求
return webClient.post()
.uri(uploadUrl)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(multipartBody))
.retrieve()
.bodyToMono(String.class)
.doOnSuccess(response -> System.out.println("上传成功: " + response))
.doOnError(error -> System.err.println("上传失败: " + error.getMessage()));

} catch (IOException e) {
return Mono.error(new RuntimeException("Excel文件处理失败", e));
}
}

/**
* 上传Excel文件(简化版本,仅包含文件)
*/
public Mono<String> uploadExcel(Workbook workbook, String fileName, String uploadUrl) {
return uploadExcel(workbook, fileName, null, uploadUrl);
}

/**
* 使用DataBuffer方式上传(适用于大文件)
*/
public Mono<String> uploadExcelWithBuffer(Workbook workbook, String fileName,
Map<String, String> additionalParams, String uploadUrl) {
try {
// 将Workbook转换为字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
byte[] fileBytes = outputStream.toByteArray();
outputStream.close();

// 使用DataBuffer包装字节数组
DataBuffer dataBuffer = DataBufferUtils.wrap(fileBytes);

MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("file", dataBuffer)
.filename(fileName)
.contentType(MediaType.APPLICATION_OCTET_STREAM);

if (additionalParams != null) {
additionalParams.forEach((key, value) ->
bodyBuilder.part(key, value));
}

var multipartBody = bodyBuilder.build();

return webClient.post()
.uri(uploadUrl)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(multipartBody))
.retrieve()
.bodyToMono(String.class);

} catch (IOException e) {
return Mono.error(new RuntimeException("Excel文件处理失败", e));
}
}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/951255.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qw-33

import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.netty.http.client.HttpClient; import reactor.nett…

qw-12

import org.springframework.core.io.ByteArrayResource;import org.springframework.core.io.buffer.DataBuffer;import org.springframework.core.io.buffer.DataBufferUtils;import org.springframework.http.Media…

reactive -03

import org.springframework.core.io.buffer.DataBuffer;import org.springframework.core.io.buffer.DataBufferUtils;import org.springframework.http.codec.multipart.FilePart;import org.springframework.web.re…

reactive - 02

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.reactive.function.BodyInserters;import org.springfra…

reactive-01

package com.hyman.service; import org.springframework.core.io.ByteArrayResource;import org.springframework.core.io.buffer.DataBufferUtils;import org.springframework.http.HttpHeaders;import org.springfr…

revit api获取excursion 拉伸体的轮廓和平面sketchplane GeometryCreationUtilities

revit api获取excursion 拉伸体的轮廓和平面sketchplane GeometryCreationUtilitiespublic void GetSketchFromExtrusion() {Document doc = this.ActiveUIDocument.Document;Extrusion extrusion = doc.GetEleme…

revit api创建模型线

revit api创建模型线public void CreateModelCurve() {Document doc = this.ActiveUIDocument.Document; // 在族文档中找到名字为"Ref. Level"的标高FilteredElementCollector collector = new Filtered…

revit 创建sketchplane

revit 创建sketchplanesketchplane在创建某些图元时候可以用到 public void CreateSketchPlaneByPlane() {Document doc = this.ActiveUIDocument.Document; using(Transaction trans = new Transaction(doc, "…

revit api创建参考线

revit api创建参考线参考线可以用modelcurve转化而来 public void ChangeModelCurveToReferenceLine() { Document doc = this.ActiveUIDocument.Document; ModelCurve modelCurve = doc.GetElement(new…

【AI说HTML 04】掌握核心文档结构标签的方法

要真正掌握HTML的核心文档结构标签,关键在于理解它们如何像搭积木一样,共同构建起一个清晰、语义明确的网页骨架。下面这个流程图概括了一个网页的典型结构,帮你先建立整体印象: flowchart TDA[<!DOCTYPE html&…

revit api创建参考平面

revit api创建参考平面public void CreatReferencePlane() {Document doc = this.ActiveUIDocument.Document; if(!doc.IsFamilyDocument)return;using(Transaction transaction = new Transaction(doc, "Edit…

【AI说HTML 02】如何深刻建立对HTML的正确认知

要深刻建立对HTML的正确认知,关键在于理解它不仅仅是用来“画”出网页样子的工具,更是一套用于描述内容结构和含义的语言。下面这个表格梳理了理解HTML时需要建立的核心认知维度。认知维度 错误认知(新手常见) 正确…

【AI说HTML 03】手把手教你搭建极简HTML开发环境

为你打造一个极简且高效的HTML开发环境,其实只需要几个核心步骤。下面这张流程图概括了全过程,你可以先有一个整体的了解,再跟着后续的详细步骤操作。 flowchart TDA[下载并安装VS Code] --> B[安装必备插件<…

图元编辑 移动 旋转 镜像等

图元编辑 移动 旋转 镜像等编辑1 Document projectDoc = ActiveUIDocument.Document;using(Transaction moveColumnTran = new Transaction(projectDoc, "Move a new column to the new place")) {moveCol…

【AI说】如何深刻建立对HTML的正确认知

要深刻建立对HTML的正确认知,关键在于理解它不仅仅是用来“画”出网页样子的工具,更是一套用于描述内容结构和含义的语言。下面这个表格梳理了理解HTML时需要建立的核心认知维度。认知维度 错误认知(新手常见) 正确…

计算机组成原理核心知识点梳理

SQLAlchemy是Python中最流行的ORM(对象关系映射)框架之一,它提供了高效且灵活的数据库操作方式。本文将介绍如何使用SQLAlchemy ORM进行数据库操作。 目录 安装SQLAlchemy 核心概念 连接数据库 定义数据模型 创建数…

revit api 内置族类型 walltype

revit api 内置族类型 walltype复制墙类型 Wall wall = RevitDoc.GetElement(new ElementId(185521)) as Wall; WallType wallType = wall.WallType; ElementType duplicatedWallType = wallType.Duplicate(wallType.…

国产的编程语言

​1、非修改序列算法 这些算法不会改变它们所操作的容器中的元素。 1.1 find 和 find_if find(begin, end, value):查找第一个等于 value 的元素,返回迭代器(未找到返回 end)。 find_if(begin, end, predicate):查…

revit api族文件图元编辑 familyitem factory

revit api族文件图元编辑 familyitem factory创建拉伸实体 //创建族文档 Document familyDoc = RevitApp.NewFamilyDocument(@"C:\ProgramData\Autodesk\RVT 2014\Family Templates\Chinese\公制常规模型.rft&qu…