Spring Cloud OpenFeign 文件上传
环境:spring boot: 2.3.4.RELEASEjava: 1.8消费者maven依赖<dependency><groupId>io.github.openfeign.form</groupId><artifactId>feign-form</artifactId><version>3.0.3</version
·
环境:
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=8082
eureka.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 {
@Autowired
private 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]);
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)