reactive -03

news/2025/10/31 2:22:08/文章来源:https://www.cnblogs.com/lihuadaiyu/p/19178471

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.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class ExcelUploadService {

private final WebClient webClient;

public ExcelUploadService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.build();
}

/**
* 上传Excel文件到第三方接口
* @param workbook Excel工作簿对象
* @param fileName 文件名
* @param uploadUrl 上传接口URL
* @return 上传结果
*/
public Mono<String> uploadExcelFile(org.apache.poi.ss.usermodel.Workbook workbook,
String fileName, String uploadUrl) {
return Mono.fromCallable(() -> {
// 将Workbook转换为字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
byte[] fileBytes = outputStream.toByteArray();
outputStream.close();
return fileBytes;
})
.flatMap(fileBytes -> {
// 创建Multipart表单数据
var multipartBody = BodyInserters
.fromMultipartData("file", new ByteArrayInputStream(fileBytes))
.with("filename", fileName);

return webClient.post()
.uri(uploadUrl)
.bodyValue(multipartBody)
.retrieve()
.bodyToMono(String.class)
.onErrorMap(WebClientResponseException.class, ex ->
new RuntimeException("上传失败: " + ex.getResponseBodyAsString(), ex));
});
}

/**
* 另一种实现方式:使用FormData
*/
public Mono<String> uploadExcelWithFormData(org.apache.poi.ss.usermodel.Workbook workbook,
String fileName, String uploadUrl) {
return Mono.fromCallable(() -> {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
return outputStream.toByteArray();
})
.flatMap(fileBytes -> {
var formData = new org.springframework.util.LinkedMultiValueMap<String, Object>();
formData.add("file", new org.springframework.core.io.ByteArrayResource(fileBytes) {
@Override
public String getFilename() {
return fileName;
}
});
formData.add("filename", fileName);

return webClient.post()
.uri(uploadUrl)
.bodyValue(formData)
.retrieve()
.bodyToMono(String.class);
});
}

/**
* 使用Base64编码的方式上传
*/
public Mono<String> uploadExcelAsBase64(org.apache.poi.ss.usermodel.Workbook workbook,
String fileName, String uploadUrl) {
return Mono.fromCallable(() -> {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
byte[] fileBytes = outputStream.toByteArray();
String base64Content = Base64.getEncoder().encodeToString(fileBytes);
return base64Content;
})
.flatMap(base64Content -> {
var requestObject = new UploadRequest();
requestObject.setFileName(fileName);
requestObject.setFileContent(base64Content);
requestObject.setFileType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

return webClient.post()
.uri(uploadUrl)
.bodyValue(requestObject)
.retrieve()
.bodyToMono(String.class);
});
}

// 上传请求对象
static class UploadRequest {
private String fileName;
private String fileContent;
private String fileType;

// Getters and Setters
public String getFileName() { return fileName; }
public void setFileName(String fileName) { this.fileName = fileName; }

public String getFileContent() { return fileContent; }
public void setFileContent(String fileContent) { this.fileContent = fileContent; }

public String getFileType() { return fileType; }
public void setFileType(String fileType) { this.fileType = fileType; }
}
}

 

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

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

相关文章

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…

【AI说】HTML从零基础到精通路径

学习HTML从零基础到精通,是一个循序渐进的过程,关键在于建立正确的认知、通过实践巩固知识,并逐步关注更宏观的网页质量维度。下面我为你梳理一条清晰的路径,并附上每个阶段的学习重点和建议。 为了让你对整体学习…

revit api 加载族文件+放置族实例 创建门图元

revit api 加载族文件+放置族实例 创建门图元string doorTypeName = "0762 x 2032 mm"; FamilySymbol doorType = null;// 在文档中找到名字为"0762 x 2032 mm"的门类型 ElementFilter doorCateg…

一件有关山寨iPod shuffle 2的往事

说起来,我其实在这两个月才知道,我当年买的是iPod shuffle 2的一个山寨货。 其实那会我们连苹果都不太清楚是啥,只知道是一家手机厂商。 记得差不多是2015-2017年那会,应该是2016年到2017年这段时间,当时我的手机…

revit api共享参数

revit api共享参数获取共享参数 // 打开共享参数文件 DefinitionFile definitionFile = RevitApp.OpenSharedParameterFile(); // 获取参数组的集合 DefinitionGroups groups = definitionFile.Groups;foreach (Defini…