SpringCloudFeign文件传输
本篇文章给大家分享的是有关Spring Cloud Feign文件传输,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联公司是一家专业的成都网站建设公司,我们专注网站设计、网站制作、网络营销、企业网站建设,外链,一元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。
一、配置文件解析器
服务提供者和消费者都需要配置文件解析器,这里使用 commons-fileupload 替换原有的解析器:
依赖:
commons-fileupload commons-fileupload 1.3.1
注入 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 ResultuploadFile(@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长度限制。
三、服务消费者配置
依赖:
io.github.openfeign.form feign-form-spring 3.2.2 io.github.openfeign.form feign-form 3.2.2
文件编码配置:
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 ObjectFactorymessageConverters; @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) ResultuploadFile(@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 ResulttestUpload(HttpServletRequest request, Long id){ Result result = null; MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; Map fileMap = mRequest.getFileMap(); for (MultipartFile mFile : fileMap.values()) { String fileName = mFile.getOriginalFilename(); result = uploadService.uploadFile(mFile, id); } return result; }
以上就是Spring Cloud Feign文件传输,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网页标题:SpringCloudFeign文件传输
文章地址:http://pwwzsj.com/article/pgishj.html