最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
使用Feign怎么實(shí)現(xiàn)多文件上傳功能

本篇文章為大家展示了使用Feign怎么實(shí)現(xiàn)多文件上傳功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供江城網(wǎng)站建設(shè)、江城做網(wǎng)站、江城網(wǎng)站設(shè)計(jì)、江城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、江城企業(yè)網(wǎng)站模板建站服務(wù),十多年江城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

想用Feign實(shí)現(xiàn)多文件的批量上傳 Controller實(shí)現(xiàn)如下代碼:

@PostMapping(value = "/uploadBatch/ali", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, headers = "content-type=multipart/form-data")
	@ApiOperation(value = "阿里云批量上傳文件")
	@ResponseBody
	public Response uploadBatchAli(
			@RequestPart("files") MultipartFile[] files,
			@RequestParam("path") String path) {
		return fileServiceClient.uploadBatchAli(files, path);
	}

FeignClient代碼如下

@PostMapping(value = File_Service_API_PREFIX + "/uploadBatch/ali", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	Response uploadBatchAli(
			@RequestPart("files") MultipartFile[] files,
			@RequestParam("path") String path);

Feign調(diào)用服務(wù),傳送類(lèi)似MultipartFile[] files的時(shí)候,會(huì)出現(xiàn)如下錯(cuò)誤

Could not write request: no suitable HttpMessageConverter found for request type [[Lorg.springframework.web.multipart.MultipartFile;] and content type [multipart/form-data]"

錯(cuò)誤是因?yàn)镕eign在組裝MultipartFile[] files,的時(shí)候出現(xiàn)了問(wèn)題 ,解決這個(gè)問(wèn)題可以重寫(xiě)SpringFormEncoder這個(gè)類(lèi),重寫(xiě)后的代碼如下

public class SpringMultipartEncoder extends SpringFormEncoder {
	public SpringMultipartEncoder(Encoder delegate) {
		super(delegate);
		MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
		processor.addWriter(new SpringSingleMultipartFileWriter());
		processor.addWriter(new SpringManyMultipartFilesWriter());
	}

	@Override
	public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
		if (bodyType != null && 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);
	}
}

然后配置類(lèi)如下:

@Configuration
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory messageConverters;

    @Bean
    public Encoder feignFormEncoder() {
        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
    }

然后FeignClient指定一下調(diào)用類(lèi)

@FeignClient(name = "xxx",
		configuration = MultipartSupportConfig.class)
public interface FileServiceClient {

@PostMapping(value = File_Service_API_PREFIX + "/uploadBatch/ali", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	Response uploadBatchAli(
			@RequestPart("files") MultipartFile[] files,
			@RequestParam("path") String path);
		    
}

上述內(nèi)容就是使用Feign怎么實(shí)現(xiàn)多文件上傳功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


新聞名稱(chēng):使用Feign怎么實(shí)現(xiàn)多文件上傳功能
本文URL:http://fisionsoft.com.cn/article/ijjesc.html