使用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);
    }
相关推荐
wuyk55538 分钟前
12.归并排序:分治思想的稳定排序算法
开发语言·数据结构·算法·排序算法
吴声子夜歌42 分钟前
ApacheCommons——commons-text(模板替换与文本算法)
java·开发语言·算法·apache
Haooog1 小时前
Agent 开发中的 Memory:State、短期记忆、长期记忆与 Memory Retrieval
java·agent·memory
砚底藏山河1 小时前
【量化纯GET实战 #23】多股票相关性:用收益率看板块联动
java·数据库·python·金融·数据分析
抠脚小弟1 小时前
Spring Cache 使用详解:从入门到实战
java·spring
ZGG0031 小时前
MySQL 索引详解:B+ 树、聚簇索引与最左前缀
java·数据库·mysql
quantdash_cc1 小时前
量化数据源怎么选:稳定性、实时性和数据完整性到底该如何权衡?
开发语言·python·数据分析·量化交易·股票数据·quantdash
萧瑟余晖2 小时前
Java深入解析篇五十四之对象模型详解
java·开发语言
张文是假的啊2 小时前
Java | record | Controller逻辑
java·开发语言
勿忘,瞬间2 小时前
Mybatis高阶
java·数据库·mybatis