网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Feign中怎么利用httpclient实现文件上传

本篇文章为大家展示了Feign中怎么利用httpclient实现文件上传,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联公司主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务顺河,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

Requirements

The feign-form extension depend on OpenFeign and its concrete versions:

  • all feign-form releases before 3.5.0works with OpenFeign 9.*versions;

  • starting from feign-form's version 3.5.0, the module works with OpenFeign 10.1.0versions and greater.

IMPORTANT:there is no backward compatibility and no any gurantee that the feign-form's versions after 3.5.0work with OpenFeign before 10.*. OpenFeign was refactored in 10th release, so the best approach - use the freshest OpenFeign and feign-form versions.

Notes:

  • spring-cloud-openfeign uses OpenFeign 9.*till v2.0.3.RELEASEand 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 deprecateddependency and it always uses the OpenFeign's 9.*versions.

使用了如下版本pom,接口httpclient下文件上传内容被篡改问题

feign-version = 9.6.0

    io.github.openfeign.form
    feign-form
    3.4.1


    io.github.openfeign.form
    feign-form-spring
    3.4.1


			io.github.openfeign
			feign-httpclient
			${feign.version}
		
		
			io.github.openfeign
			feign-core
			${feign.version}
		
		
			io.github.openfeign
			feign-hystrix
			${feign.version}
		
		
			io.github.openfeign
			feign-slf4j
			${feign.version}
		

多附件文件上传问题

默认SpringFormEncoder只处理单文件,一个强转就完事了

@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);
}

我们系统文件上传支持多附件,这个需要重写下

/**
 * @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 {

}

上述内容就是Feign中怎么利用httpclient实现文件上传,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站栏目:Feign中怎么利用httpclient实现文件上传
标题路径:http://bjjierui.cn/article/jscddd.html

其他资讯