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

Spring Cloud Feign的文件上传实现

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

Spring Cloud Feign的文件上传实现

在Spring Cloud封装的Feign中并不直接支持传文件,但可以通过引入Feign的扩展包来实现,本来就来具体说说如何实现。

原文:http://blog.didispace.com/spring-cloud-starter-dalston-2-4/

服务提供方(接收文件)

服务提供方的实现比较简单,就按Spring MVC的正常实现方式即可,比如:

@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class Application {    @RestController
    public class UploadController {        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)        public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {            return file.getName();
        }

    }    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}
服务消费方(发送文件)

在服务消费方由于会使用Feign客户端,所以在这里需要在引入feign对表单提交的依赖,具体如下:


   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

定义文件上传方的应用主类和FeignClient,假设服务提供方的服务名为eureka-feign-upload-server

@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}@FeignClient(value = "upload-server", configuration = UploadService.MultipartSupportConfig.class)public interface UploadService { 
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)    String handleFileUpload(@RequestPart(value = "file") MultipartFile file); 
    @Configuration
    class MultipartSupportConfig {        @Bean
        public Encoder feignFormEncoder() {            return new SpringFormEncoder();
        }
    }
 
}

在启动了服务提供方之后,尝试在服务消费方编写测试用例来通过上面定义的Feign客户端来传文件,比如:

@Slf4j@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTestpublic class UploadTester {    @Autowired
    private UploadService uploadService;    @Test
    @SneakyThrows
    public void testHandleFileUpload() {

        File file = new File("upload.txt");
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
                MediaType.TEXT_PLAIN_VALUE, true, file.getName());        try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
            IOUtils.copy(input, os);
        } catch (Exception e) {            throw new IllegalArgumentException("Invalid file: " + e, e);
        }

        MultipartFile multi = new CommonsMultipartFile(fileItem);

        log.info(uploadService.handleFileUpload(multi));
    }

}
完整示例:

读者可以根据喜好选择下面的两个仓库中查看eureka-feign-upload-server和eureka-feign-upload-client两个项目:

  • Github:https://github.com/dyc87112/SpringCloud-Learning/

  • Gitee:https://gitee.com/didispace/SpringCloud-Learning/

原文出处

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

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

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