栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud OpenFeign 文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud OpenFeign 文件上传

环境:

spring boot: 2.3.4.RELEASE
java: 1.8

消费者 maven依赖

    io.github.openfeign.form
    feign-form
    3.0.3


    io.github.openfeign.form
    feign-form-spring
    3.0.3


    commons-fileupload
    commons-fileupload
    1.3.3

参数配置
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 multiValueMap);

    class MultipartSupportConfig{

        @Bean
        @Primary
        @Scope("prototype")
        public Encoder feignFormEncoder(ObjectFactory 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 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]);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346466.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号