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

打印 上一主题 下一主题

主题 967|帖子 967|积分 2901

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
介绍: A项目调用B项目接口并提供与B雷同接口
  1.     @PostMapping
  2.     @ApiOperation("add")
  3.     public AjaxResult add(@RequestParam(value = "file") MultipartFile file, RemoteSense remoteSense) throws IOException {
  4.         if (file == null || file.isEmpty()) {
  5.             return AjaxResult.error("文件不能为空");
  6.         }
  7.         // 将 RemoteSense 对象转换为 Map 以便传递参数
  8.         Map<String, Object> remoteSenseParams = BeanUtil.beanToMap(remoteSense, false, true);
  9.         // 将 InputStream 封装为 Hutool 的 Resource
  10.         Resource fileResource = new InputStreamResource(file.getInputStream(), file.getOriginalFilename());
  11.         // 使用 Hutool 的 form 方法传递 Resource
  12.         HttpResponse response = HttpRequest.post(REMOTE_SENSE_BASE_URL)
  13.                 .form("file", fileResource)  // 直接传递 InputStreamResource 代替 File
  14.                 .form(remoteSenseParams)  // 添加其他参数
  15.                 .execute();
  16.         return JSON.parseObject(response.body(), AjaxResult.class);
  17.     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表