使用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);
    }
相关推荐
时间的拾荒人39 分钟前
C语言字符函数与字符串函数完全指南
c语言·开发语言
帅次40 分钟前
Android 高级工程师面试:Java 基础知识 近1年高频追问 22 题
android·java·面试
蓝胖的四次元口袋42 分钟前
Java集合(4)
java·哈希算法
2501_948106911 小时前
计算机毕业设计之基于jsp教科研信息共享系统
java·开发语言·信息可视化·spark·课程设计
TanYYF1 小时前
spring ai入门教程二
java·人工智能·spring
SeeYa-J1 小时前
Spring IOC(Inversion of Control)
java·spring·rpc
取经蜗牛1 小时前
Python 第一阶段完全指南:从零到第一个实用工具
开发语言·python
不会c+2 小时前
02-SpringBoot配置文件
java·spring boot·后端
AI 大模型学习不踩坑2 小时前
OpenClaw 完整教程:从安装到使用(官方脚本版)
java·人工智能·神经网络·机器学习·计算机视觉·自然语言处理·openclaw
dog2502 小时前
从重尾到截断流量模型的演进
开发语言·php