您好,登錄后才能下訂單哦!
本篇文章為大家展示了Feign中怎么利用httpclient實現(xiàn)文件上傳,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
The feign-form
extension depend on OpenFeign
and its concrete versions:
all feign-form
releases before 3.5.0 works with OpenFeign
9.* versions;
starting from feign-form
's version 3.5.0, the module works with OpenFeign
10.1.0 versions and greater.
IMPORTANT: there is no backward compatibility and no any gurantee that the
feign-form
's versions after 3.5.0work withOpenFeign
before 10.*.OpenFeign
was refactored in 10th release, so the best approach - use the freshestOpenFeign
andfeign-form
versions.
Notes:
spring-cloud-openfeign uses OpenFeign
9.* till v2.0.3.RELEASE and uses 10.* after. Anyway, the dependency already has suitable feign-form
version, see dependency pom, so you don't need to specify it separately;
spring-cloud-starter-feign
is a deprecated dependency and it always uses the OpenFeign
's 9.* versions.
使用了如下版本pom,接口httpclient下文件上傳內(nèi)容被篡改問題
feign-version = 9.6.0 <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>${feign.version}</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>${feign.version}</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-hystrix</artifactId> <version>${feign.version}</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-slf4j</artifactId> <version>${feign.version}</version> </dependency>
默認SpringFormEncoder只處理單文件,一個強轉(zhuǎn)就完事了
@Override public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (!bodyType.equals(MultipartFile.class)) { super.encode(object, bodyType, template); return; } val file = (MultipartFile) object; val data = singletonMap(file.getName(), object); super.encode(data, MAP_STRING_WILDCARD, template); }
我們系統(tǒng)文件上傳支持多附件,這個需要重寫下
/** * @author yugj * @date 2019/8/28 下午4:27. */ 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", object); super.encode(data, MAP_STRING_WILDCARD, template); return; } } super.encode(object, bodyType, template); } }
對應feignclient使用這個配置即可
@Service @FeignClient(name = "xx-file-sv", fallbackFactory = FileGeneralHystrixFallbackAdapter.class, configuration = FeignSpringFormEncoder.class) public interface IFileClient extends IFile { }
上述內(nèi)容就是Feign中怎么利用httpclient實現(xiàn)文件上傳,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。