美文网首页
Feign 实现微服务文件上传

Feign 实现微服务文件上传

作者: 逆水寻洲 | 来源:发表于2019-06-26 10:06 被阅读0次

    在Spring Cloud 的Feign组件中并不支持文件的传输。这里记录一下个人的学习学习踩坑

    服务提供者

    文件上传功能代码

    @RequestMapping(value = {"/image"}, method = {RequestMethod.POST}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        @ApiOperation(value = "上传图片")
        public ResponseData uploadImage(@ApiParam("图片数据") @RequestParam("image") MultipartFile file, @NotNull(message = "path不能为空") @ApiParam("保存路径") String path) {
            String imagePath = null;
            try {
                if (file == null || file.isEmpty()) {
                    logger.warn("找不到上传的图片");
                    return new ResponseData(404, "你总得把图片给我吧");
                }
                //上传的文件名
                String filename = file.getOriginalFilename();
                //获取文件的后缀
                String suffix = filename.substring(filename.lastIndexOf("."));
    
                if (allowUploadImages.indexOf(suffix.replace(".", "")) >= 0) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
                    String pathDir = "/images/" + path + "/" + dateFormat.format(new Date());
                    filename = UUID.randomUUID().toString() + (suffix.trim());
                    String realPath = staticLocations + pathDir;
                    File saveFile = new File(new File(realPath), filename);
                    if (!saveFile.getParentFile().exists()) {
                        saveFile.getParentFile().mkdirs();
                    }
                    file.transferTo(saveFile);
                    imagePath = pathDir + "/" + filename;
                } else {
                    logger.warn("非法图片格式");
                    return new ResponseData(500, "这种图片格式我可不认");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ResponseData(200, "success", imagePath);
        }
    
    Feign客户端
    1. 导入Feign文件上传解析依赖
    <!-- feign 文件上传功能-->
            <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>
    
    1. 编写Feign 文件上传服务
    @FeignClient(value = "upload-service",configuration= MultipartSupportConfig.class)
    public interface UploadFeignService {
    
        @RequestMapping(value = {"/sys/upload/image"}, method = {RequestMethod.POST},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        ResponseData uploadImage(@RequestPart(value = "file") MultipartFile file, @NotNull(message = "path不能为空") @RequestParam("path") String path);
    }
    
    1. 文件上传支持配置文件
    public class MultipartSupportConfig {
    
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
    

    服务消费者调用

    @Autowired
        private UploadFeignService uploadFeignService;
    
        @RequestMapping(value = {"/image"}, method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
        @ApiOperation(value = "上传图片")
        public ResponseData uploadImage(@ApiParam("图片数据") @RequestParam("image") MultipartFile file, @NotNull(message = "path不能为空") @ApiParam("保存路径") String path) {
            System.out.println("=======图片上传=========");
            ResponseData responseData = uploadFeignService.uploadImage(file, path);
            return responseData;
        }
    

    相关文章

      网友评论

          本文标题:Feign 实现微服务文件上传

          本文链接:https://www.haomeiwen.com/subject/yesqcctx.html