feign调用上传文件接口(MultipartFile)
程序员文章站
2022-06-26 08:05:54
...
1.服务端MultipartFile这个类型的参数要用@RequestPart(“file”) 这个注解
@PostMapping("/uploadMultipleFile")
public Result uploadImg(@RequestPart("file") MultipartFile[] files){
try {
return uploadService.uploadImg(files);
}catch (Exception e){
log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
return new Result(ResultCode.FAIL,"文件上传异常");
}
}
2.调用端加入如下坐标
<!--feign-form-->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<!--配合spring使用-->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.8.0</version>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.加入配置类
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
@Configuration
public class MultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
4.调用端feign接口
import com.qianxian.common.util.Result;
import com.qianxian.user.config.MultipartSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* 上传接口
* @author yyj
*/
@FeignClient(name="ldd-upload",configuration = MultipartSupportConfig.class)
public interface UploadControllerFeign {
/**
* 图片上传
* @param files
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/uploadMultipleFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result uploadImg(@RequestPart("file") MultipartFile[] files);
}
5.controller层调用
@PostMapping("/uploadMultipleFile")
public Result uploadImg(@RequestPart("file") MultipartFile[] files){
try {
return uploadControllerFeign.uploadImg(files);
}catch (Exception e){
log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
return new Result(ResultCode.FAIL,"文件上传异常");
}
}
下一篇: 数据结构和算法部分
推荐阅读
-
SpringMVC使用MultipartFile实现文件上传
-
java 调用Spring接口上传文件及其他参数填充
-
js实现文件上传功能 后台使用MultipartFile
-
一句代码上传MultipartFile图片到指定文件夹
-
PHP调用wsdl文件类型的接口代码分享
-
php用windows COM组件调用openoffice接口实现word转pdf文件时报错的解决办法
-
Spring Cloud Feign的文件上传实现的示例代码
-
记Laravel调用Gin接口调用formData上传文件的实现方法
-
(springCloud-8 Zuul网关路由的基本配置feign实现接口调用)
-
Spring Cloud下使用Feign Form实现微服务之间的文件上传