开局一张图
一. 服务双方
服务名 | 角色 |
nanniwan-system | feign服务提供者 |
nanniwan-finance | feign服务消费者 |
二. 服务提供者接口及配置
注意:用
RequestPart
绑定参数
/**
* 文件上传
*
* @param file
* @return
* @throws IOException
*/
@PostMapping(value = "uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Map<String, String> uploadFile(@RequestPart("file") MultipartFile file) throws IOException {
return upload(file);
}
三. 服务消费者接口以及配置
1.查看项目是否已经引入如下三个jar,如果没有就需要配置:
pom文件引入jar包
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2.接口请求服务提供者,
重点在这里
: // 这里是从url拿到的文件,文件的来源有几种:比如1:前端传递(MultipartFile类型),2,根据路径获取(file类型),
最终都要转成流
(InputStream)
public int insertInvoceEtc(InvoiceEtc invoiceEtc) {
try {
String filePath = invoiceEtc.getFilePath();
String invoiceNum = invoiceEtc.getInvoiceNum();
String invoiceCode = invoiceEtc.getInvoiceCode();
String fileName = invoiceNum + invoiceCode + ".pdf";
// 这里是从url拿到的文件,文件的来源有几种:比如1:前端传递(MultipartFile类型),2,根据路径获取(file类型),最终都要转成流(InputStream)
URL url = new URL(filePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为5秒
conn.setConnectTimeout(5 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
InputStream inputStream = conn.getInputStream();
MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, "application/pdf", inputStream);
Map<String, String> fileMap = remoteCommonService.upload(multipartFile);
if (inputStream != null) {
inputStream.close();
}
if (CollectionUtil.isNotEmpty(fileMap)) {
String localFilePath = fileMap.get("url");
invoiceEtc.setLocalFilePath(localFilePath);
invoiceEtc.setFileName(fileName);
}
} catch (Exception e) {
log.info("insertInvoceEtc 附件信息:=============================" + e.getMessage());
}
return 0;
}
四:feign接口类
package com.nanniwan.system.feign;
import com.nanniwan.common.constant.ServiceNameConstants;
import com.nanniwan.system.config.FeignSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
@FeignClient(name = ServiceNameConstants.SYSTEM_SERVICE, configuration = FeignSupportConfig.class)
public interface RemoteCommonService {
@PostMapping(value = "/common/transform", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String transform(@RequestPart("file") MultipartFile multipartFile);
/**
* 上传文件到服务器
* @param file
* @return
*/
@PostMapping(value = "/oss/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Map<String, String> upload(@RequestPart("file") MultipartFile file);
}
五. feign实现文件上传的配置类
package com.nanniwan.system.config;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
public class FeignSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
版权声明:本文为buyaopingbixiazai原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。