您好,登录后才能下订单哦!
本篇文章给大家分享的是有关Spring Cloud Feign文件传输,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
一、配置文件解析器
服务提供者和消费者都需要配置文件解析器,这里使用 commons-fileupload 替换原有的解析器:
依赖:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
注入 bean :
@Bean(name = "multipartResolver") public MultipartResolver mutipartResolver(){ CommonsMultipartResolver com = new CommonsMultipartResolver(); com.setDefaultEncoding("utf-8"); return com; }
程序入口中剔除原有的解析器:
@SpringBootApplication(exclude = {MultipartAutoConfiguration.class})
二、服务提供者,即接收文件一方的配置
Controller 的写法:
@ResponseBody @RequestMapping(value = "/upload", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Result<String> uploadFile(@RequestPart("file")MultipartFile file, @RequestParam("id")Long id){ String fileName = file.getOriginalFilename(); String extend = FileOperateUtil.suffix(fileName); FileOperateUtil.copy("E:\\" + fileName, file); return ResultBuilder.success("ok"); }
@RequestPart 指定文件,后面的 @RequestParam 是额外参数,注意额外参数不能超过url长度限制。
三、服务消费者配置
依赖:
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.2.2</version> </dependency>
文件编码配置:
import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.cloud.netflix.feign.support.SpringEncoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MultipartSupportConfig{ @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder(){ return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
Feign 接口定义:
@FeignClient(name = "test-upload") public interface UploadService{ @ResponseBody @RequestMapping(value = "/upload", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result<String>uploadFile(@RequestPart("file")MultipartFile file, @RequestParam("id")Long id); }
与普通 Feign 接口写法差不多,注意方法注解和参数与服务提供者的 controller
一样。
Controller
的写法, Controller
中接收前端传过来的文件信息和额外参数,然后通过 Feign 接口传输到远端:
// 注入 feign 接口 @Autowired private UploadService uploadService; @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json; charset=utf-8") @ResponseBody public Result<String> testUpload(HttpServletRequest request, Long id){ Result<String> result = null; MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = mRequest.getFileMap(); for (MultipartFile mFile : fileMap.values()) { String fileName = mFile.getOriginalFilename(); result = uploadService.uploadFile(mFile, id); } return result; }
以上就是Spring Cloud Feign文件传输,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。