使用Hutoo库HttpRequest工具类调用MultipartFile参数接口

介绍: A项目调用B项目接口并提供与B相同接口

java 复制代码
    @PostMapping
    @ApiOperation("add")
    public AjaxResult add(@RequestParam(value = "file") MultipartFile file, RemoteSense remoteSense) throws IOException {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("文件不能为空");
        }

        // 将 RemoteSense 对象转换为 Map 以便传递参数
        Map<String, Object> remoteSenseParams = BeanUtil.beanToMap(remoteSense, false, true);

        // 将 InputStream 封装为 Hutool 的 Resource
        Resource fileResource = new InputStreamResource(file.getInputStream(), file.getOriginalFilename());

        // 使用 Hutool 的 form 方法传递 Resource
        HttpResponse response = HttpRequest.post(REMOTE_SENSE_BASE_URL)
                .form("file", fileResource)  // 直接传递 InputStreamResource 代替 File
                .form(remoteSenseParams)  // 添加其他参数
                .execute();

        return JSON.parseObject(response.body(), AjaxResult.class);
    }
相关推荐
Ting-yu几秒前
Spring AI Alibaba零基础速成(1) ---- 项目创建与配置
java·人工智能·spring
喜欢coding的谢同学2 分钟前
ArthasClaw:用自然语言诊断 JVM 的 AI 助手,告别繁琐的 Arthas 命令
java·人工智能·arthas
陳土2 分钟前
R语言jiebaR包使用摘要
开发语言·r语言
Evand J3 分钟前
【MATLAB】多无人机编队协同控制与三维航迹规划仿真。障碍物斥力避障,输出编队误差、控制输入、三维轨迹等
开发语言·matlab·无人机
froginwe1120 分钟前
jQuery UI 小部件方法调用
开发语言
信奥胡老师22 分钟前
B3930 [GESP202312 五级] 烹饪问题
开发语言·数据结构·c++·学习·算法
架构源启26 分钟前
Spring AI完整学习路线:从Java开发到AI Agent的进阶之路(附15篇实战教程)
java·人工智能·spring
SPC的存折27 分钟前
20、K8S-Pod驱逐
java·docker·kubernetes
JAVA学习通29 分钟前
安脉盛 软件后端开发实习面经
java·开发语言
sycmancia36 分钟前
Qt——Qt中的事件处理(一)
开发语言·qt