OpenFeign FormData
服务端接口代码如下:
/** * 上传数据+实体信息 */ @RequestMapping("/upload") public String doctorAnalysis(HttpServletRequest request, @RequestParam(value = "userinfo") String param, @RequestParam(value = "file") MultipartFile file) { logger.info("userinfo json => {}", param); logger.info("上传成功{}", file.getOriginalFilename()); File saveFile = new File("D:\\"+file.getOriginalFilename()); if (!saveFile.exists()) { saveFile.mkdirs(); } try { file.transferTo(saveFile); } catch (IOException e) { logger.error(e.getMessage(), e); e.printStackTrace(); } return "上传成功"; }
PostMan 如下
OpenFeign
@FeignClient(name = "vipsoft", url = "${api.url}") public interface ICallbackFeignService { /** * 上传文件 * * 注意: 使用openfeign传递参数含有文件类型时必须指定 consumes = MediaType.MULTIPART_FORM_DATA_VALUE * * @param param 需要将对象转成JSON,如果直接传对象出去,将会变成 Key Value的形式 * @return */ @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String upload(@RequestPart("result") String param, @RequestPart("file") MultipartFile file); }
package com.vipsoft.web; import com.vipsoft.web.rpc.ICallbackFeignService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; @SpringBootTest public class AppCallbackTest { @Autowired ICallbackFeignService appCallbackFeignService; @Test void Upload() throws Exception { File file = new File("D:\\Users\\Desktop\\fanye.mp4"); //这里的第一个参数值 file 是对应上面feign的文件注解中的@RequestPar中的name。一定要对应上 MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "application/octet-stream;charset=utf-8", new FileInputStream(file)); UserInfo param = new UserInfo(); param.setUserName("张三"); param.setAge(40); appCallbackFeignService.upload(multipartFile); } }
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/16331172.html
分类:
JAVA
标签:
SpringBoot
, SpringCloud
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2021-05-31 Docker 常用命令
2021-05-31 Nginx 安装配置
2021-05-31 Docker 安装 Nginx