使用Feign擴(kuò)展包怎么實(shí)現(xiàn)微服務(wù)間文件上傳?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)公司始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營(yíng)理念,通過(guò)多達(dá)10年累計(jì)超上千家客戶(hù)的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的推廣解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶(hù),其中包括:玻璃隔斷等企業(yè),備受客戶(hù)贊揚(yáng)。
單文件上傳
2.1 feign_upload_first服務(wù)提供者
文件上傳的服務(wù)提供者接口比較簡(jiǎn)單,如下所示:
@SpringBootApplication public class FeignUploadFirstApplication { @RestController public class UploadController { @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) { return file.getOriginalFilename(); } } public static void main(String[] args) { SpringApplication.run(FeignUploadFirstApplication.class, args); } }
2.2 feign_upload_second服務(wù)消費(fèi)者
增加擴(kuò)展包依賴(lài)
io.github.openfeign.form feign-form 3.3.0 io.github.openfeign.form feign-form-spring 3.3.0 commons-fileupload commons-fileupload 1.3.3
新增feign實(shí)現(xiàn)文件上傳的配置類(lèi)
@Configuration public class FeignSupportConfig { @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(); } }
feign遠(yuǎn)程調(diào)用接口
@FeignClient(name = "file",url = "http://localhost:8100",configuration = FeignSupportConfig.class) public interface UploadService { @RequestMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String handleFileUpload(@RequestPart(value = "file") MultipartFile file); }
上傳文件接口
@RestController public class UploadController { @Autowired UploadService uploadService; @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) { return uploadService.handleFileUpload(file); } }
2.3 測(cè)試
使用postman進(jìn)行測(cè)試,可以正常上傳文件
三、多文件上傳
既然單個(gè)文件可以上傳,那么多文件應(yīng)該也沒(méi)問(wèn)題吧,我們對(duì)上面的代碼進(jìn)行修改
3.1 feign_upload_first服務(wù)提供者
文件上傳的服務(wù)提供者接口比較簡(jiǎn)單,如下所示:
@SpringBootApplication public class FeignUploadFirstApplication { @RestController public class UploadController { @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) { return file.getOriginalFilename(); } @RequestMapping(value = "/uploadFile2",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload(@RequestPart(value = "file") MultipartFile[] file) { String fileName = ""; for(MultipartFile f : file){ fileName += f.getOriginalFilename()+"---"; } return fileName; } } public static void main(String[] args) { SpringApplication.run(FeignUploadFirstApplication.class, args); } }
3.2 feign_upload_second服務(wù)消費(fèi)者
feign遠(yuǎn)程調(diào)用接口
@FeignClient(name = "file",url = "http://localhost:8100",configuration = FeignSupportConfig.class) public interface UploadService { @RequestMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String handleFileUpload(@RequestPart(value = "file") MultipartFile file); @RequestMapping(value = "/uploadFile2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String handleFileUpload(@RequestPart(value = "file") MultipartFile[] file); }
上傳文件接口
@RestController public class UploadController { @Autowired UploadService uploadService; @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) { return uploadService.handleFileUpload(file); } @RequestMapping(value = "/uploadFile2",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String handleFileUpload2(@RequestPart(value = "file") MultipartFile[] file) { return uploadService.handleFileUpload(file); } }
3.3 測(cè)試
經(jīng)過(guò)測(cè)試發(fā)現(xiàn),無(wú)法上傳多個(gè)文件。經(jīng)過(guò)檢查,發(fā)現(xiàn)源碼里底層是有對(duì)MultipartFile[]類(lèi)型的支持的,源碼中有個(gè)類(lèi)叫SpringManyMultipartFilesWriter,是專(zhuān)門(mén)針對(duì)文件數(shù)組類(lèi)型進(jìn)行操作的,但是配置到項(xiàng)目里的SpringFormEncoder類(lèi)里卻沒(méi)有對(duì)文件數(shù)組類(lèi)型的判斷,以致不能支持文件數(shù)組的上傳
SpringManyMultipartFilesWriter源碼
public class SpringManyMultipartFilesWriter extends AbstractWriter { private final SpringSingleMultipartFileWriter fileWriter = new SpringSingleMultipartFileWriter(); public SpringManyMultipartFilesWriter() { } public void write(Output output, String boundary, String key, Object value) throws Exception { if (value instanceof MultipartFile[]) { MultipartFile[] files = (MultipartFile[])((MultipartFile[])value); MultipartFile[] var6 = files; int var7 = files.length; for(int var8 = 0; var8 < var7; ++var8) { MultipartFile file = var6[var8]; this.fileWriter.write(output, boundary, key, file); } } else if (value instanceof Iterable) { Iterable> iterable = (Iterable)value; Iterator var11 = iterable.iterator(); while(var11.hasNext()) { Object file = var11.next(); this.fileWriter.write(output, boundary, key, file); } } } public boolean isApplicable(Object value) { if (value == null) { return false; } else if (value instanceof MultipartFile[]) { return true; } else { if (value instanceof Iterable) { Iterable> iterable = (Iterable)value; Iterator> iterator = iterable.iterator(); if (iterator.hasNext() && iterator.next() instanceof MultipartFile) { return true; } } return false; } } }
SpringFormEncoder源碼
public class SpringFormEncoder extends FormEncoder { public SpringFormEncoder() { this(new Default()); } public SpringFormEncoder(Encoder delegate) { super(delegate); MultipartFormContentProcessor processor = (MultipartFormContentProcessor)this.getContentProcessor(ContentType.MULTIPART); processor.addWriter(new SpringSingleMultipartFileWriter()); processor.addWriter(new SpringManyMultipartFilesWriter()); } public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (!bodyType.equals(MultipartFile.class)) { super.encode(object, bodyType, template); } else { MultipartFile file = (MultipartFile)object; Mapdata = Collections.singletonMap(file.getName(), object); super.encode(data, MAP_STRING_WILDCARD, template); } } }
從上面SpringFormEncoder的源碼上可以看到SpringFormEncoder類(lèi)構(gòu)造時(shí)把SpringManyMultipartFilesWriter實(shí)例添加到了處理器列表里了,但是在encode方法里又只判斷了MultipartFile類(lèi)型,沒(méi)有判斷數(shù)組類(lèi)型,底層有對(duì)數(shù)組的支持但上層卻缺少了相應(yīng)判斷。那么我們可以自己去擴(kuò)展FormEncoder,仿照SpringFormEncoder源碼,只修改encode方法。
3.3 擴(kuò)展FormEncoder支持多文件上傳
擴(kuò)展FormEncoder,命名為FeignSpringFormEncoder
public class FeignSpringFormEncoder extends FormEncoder { /** * Constructor with the default Feign's encoder as a delegate. */ public FeignSpringFormEncoder() { this(new Default()); } /** * Constructor with specified delegate encoder. * * @param delegate delegate encoder, if this encoder couldn't encode object. */ public FeignSpringFormEncoder(Encoder delegate) { super(delegate); MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART); processor.addWriter(new SpringSingleMultipartFileWriter()); processor.addWriter(new SpringManyMultipartFilesWriter()); } @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(MultipartFile.class)) { MultipartFile file = (MultipartFile) object; Map data = Collections.singletonMap(file.getName(), object); super.encode(data, MAP_STRING_WILDCARD, template); return; } else if (bodyType.equals(MultipartFile[].class)) { MultipartFile[] file = (MultipartFile[]) object; if(file != null) { Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object); super.encode(data, MAP_STRING_WILDCARD, template); return; } } super.encode(object, bodyType, template); } }
注冊(cè)配置類(lèi)
@Configuration public class FeignSupportConfig { @Bean public Encoder feignFormEncoder() { return new FeignSpringFormEncoder(); } }
看完上述內(nèi)容,你們掌握使用Feign擴(kuò)展包怎么實(shí)現(xiàn)微服務(wù)間文件上傳的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!