Spring中和Feign中调用带有文件参数的接口的具体实现(附带feign-form中添加自定义Writer的实现)

1. Spring 中调用文件接口

  1. 导入jar
	   <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.9</version>
            <scope>test</scope>
        </dependency>
  1. 通过MultipartEntityBuilder构造请求的内容参数
public void sendFile(){
   
   	String url = "http://localhost:8588/email/sendWithAttachments";
       File file = new File("C:\\Users\\PC\\Desktop\\spring.md");
	HttpPost httpPost = new HttpPost(url);
       //FileBody bin = new FileBody(file);
       //StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

       // 传递文件有三种方式    1. 发送File 2.发送InputStream  3.发送Byte[]
       FileBody fileBody = new FileBody(file);
       InputStreamBody file2Body = new InputStreamBody(new FileInputStream(file),file.getName());
       ByteArrayBody fileByte = new ByteArrayBody(File2byte(file),file.getName());


       // 构造请求体
       HttpEntity reqEntity = MultipartEntityBuilder.create()
               //.setCharset(CharsetUtil.UTF_8)  //万恶之源,测试很多次才发现原来是这玩意影响的
               .setMode(HttpMultipartMode.RFC6532)//加上此行代码解决返回中文乱码问题
//                .setContentType(ContentType.MULTIPART_FORM_DATA)
//                .addPart("files", fileBody)
               .addPart("fileList", fileByte)
           	//邮件主题
               .addPart("subject", new StringBody("hello color",ContentType.APPLICATION_JSON))
           	// 邮件内容
               .addPart("content", new StringBody("color",ContentType.APPLICATION_JSON))
               .addPart("receiveList", new StringBody("[email protected]",ContentType.APPLICATION_JSON))
               .build();
       httpPost.setEntity(reqEntity);
       
       // 请求接口
       CloseableHttpResponse response = HttpClients.createDefault().execute(httpPost);
       HttpEntity responseEntity = response.getEntity();
       if (responseEntity != null) {
   
           // 将响应内容转换为字符串
           String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
       }
}

2. Feign调用第三方文件接口

Feign传表单数据参考了 https://blog.csdn.net/ytzzh0726/article/details/79731343 这一片文章,是通过传递Map参数来实现Feign传表单数据,本文章在此基础上,新加了传递自定义实体类来实现Feign传表单数据

1. Feign 表单提交文件jar包依赖

		<!-- 对应SpringCloud版本Finchley.RELEASE-->
		<dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.2.2</version>
        </dependency>

2. FeignClient 接口声明

@FeignClient(name = "TestApi", url = "http://localhost:8588", configuration = TestApi.FeignMultipartSupportConfig.class)
public interface TestApi {
   

    @PostMapping(path = "/email/sendWithAttachments", produces = {
   MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Object sendByMap(Map<String,Object> param);
    
    @PostMapping(path = "/email/sendWithAttachments", produces = {
   MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Object sndTestBody(MailPojo pojo);
    
    //自定义文件上传编码器
    class 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值