当前位置: 代码迷 >> 综合 >> Spring Cloud OpenFeign 文件上传
  详细解决方案

Spring Cloud OpenFeign 文件上传

热度:56   发布时间:2023-12-08 17:11:16.0

环境:

spring boot: 2.3.4.RELEASE
java: 1.8

消费者

maven依赖

<dependency><groupId>io.github.openfeign.form</groupId><artifactId>feign-form</artifactId><version>3.0.3</version>
</dependency>
<dependency><groupId>io.github.openfeign.form</groupId><artifactId>feign-form-spring</artifactId><version>3.0.3</version>
</dependency>
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.3</version>
</dependency>

参数配置

spring.application.name=consumer-application
server.port=8082eureka.client.register-with-eureka=true
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args){
    SpringApplication.run(ConsumerApplication.class, args);}
}

Feign 接口 (以下参考网络资料)

@FeignClient(value = "hello-service", configuration = HelloWorld.MultipartSupportConfig.class)
public interface HelloWorld {
    @PostMapping(value = "/upload/file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)String upload(MultiValueMap<String, Object> multiValueMap);class MultipartSupportConfig{
    @Bean@Primary@Scope("prototype")public Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
    return new SpringFormEncoder(new SpringEncoder(converters));}}
}

Controller

@Controller
@Slf4j
public class UploadController {
    @Autowiredprivate HelloWorld helloWorld;@PostMapping("/upload/file")public String upload(UploadBO uploadBO) {
    File tempFile = null;try{
    tempFile = Files.createTempFile("galaxy_file_temp",uploadBO.getFile().getOriginalFilename()).toFile();uploadBO.getFile().transferTo(tempFile);} catch (IOException ex) {
    ex.printStackTrace();}FileSystemResource fileSystemResource = new FileSystemResource(tempFile);log.info("file name is : {}", uploadBO.getFileName());MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();multiValueMap.add("file", fileSystemResource);multiValueMap.add("fileName", uploadBO.getFileName());multiValueMap.add("fileSize", uploadBO.getFileSize());return helloWorld.upload(multiValueMap);

提供者

Controller

@Controller
@Slf4j
public class UploadController {
    @PostMapping("/upload/file")public String upload(UploadBO uploadBO) {
    log.info("file name is : {}", uploadBO.getFileName());File tempFile = null;try{
    tempFile = Files.createTempFile("galaxy_file_temp",uploadBO.getFile().getOriginalFilename()).toFile();log.info("{}", tempFile.getAbsolutePath());uploadBO.getFile().transferTo(tempFile);} catch (IOException ex) {
    ex.printStackTrace();}return "success";}
}
@Data
public class UploadBO {
    private String fileName;private Integer fileSize;private MultipartFile file;
}

以上测试通过,但移到公司项目上时却无法成功,经反复测试是由于公司项目上一个服务有多个client,当针对同一个服务的某个feign client配置configuration时,configuration未能生效,由于公司项目不便升级故使用restTemplate发送请求。

https://www.jianshu.com/p/e94fc3ca3295

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FOMR_DATA);
HttpEntity requestEntity = new HttpEntity(multiValueMap, headers);
restTemplate.postForEntity(url, requestEntity, JSONObject.class, new Object[0]);
  相关解决方案