Feign跨服务上传文件

1.feign multipart 跨服务传递文件配置类(非全局配置)

public class FeignMultipartConfig {

    @Bean
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
            @Override
            public HttpMessageConverters getObject() throws BeansException {
                return new HttpMessageConverters(new RestTemplate().getMessageConverters());
            }
        }));
    }

}

2.文件服务feign client

@FeignClient(name = "msfileupload", decode404 = true,configuration = FeignMultipartConfig.class)
public interface MsFileUploadFeignApi {

    @RequestMapping(value = "/upload/single", method = RequestMethod.POST,  produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    UploadResponse singleUpload(@RequestParam("bizType") String bizType, @RequestPart("file") MultipartFile file);

    @RequestMapping(value = "/upload/multi", method = RequestMethod.POST,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    BaseRestResponse multiUpload(@RequestParam("bizType") String bizType, @RequestPart("files") MultipartFile[] files);

}

3.当前服务暴露给外部的feign client

@FeignClient(name = "demoservice", url = "${demoservice.feign.url:}" , fallback = FileUploadCommonApiFallBack.class)
public interface FileUploadCommonApi {

    @RequestMapping(value = "/common/file/upload/single", method = RequestMethod.POST)
    UploadResponse singleUpload(@RequestParam("bizType") String bizType, @RequestParam("file") MultipartFile file);

    @RequestMapping(value = "/common/file/upload/multi", method = RequestMethod.POST)
    BaseRestResponse multiUpload(@RequestParam("bizType") String bizType
            , @RequestParam("files") MultipartFile[] files);

}

4.当前服务控制器代码片段:

@Slf4j
@RestController("fileUploadCommonControllerV1")
@Api(value = "文件通用处理", tags= "文件上传通用控制器")
public class FileUploadCommonController implements FileUploadCommonApi {

    @Autowired
    MsFileUploadFeignApi msFileUploadFeignApi;

    @ApiOperation(value = "单个文件上传", notes = "单文件上传")
    @Override
    public UploadResponse singleUpload(@RequestParam("bizType") String bizType
            , @RequestParam("file") MultipartFile file) {
        log.info("FileUploadCommonController.singleUpload ...");
        return msFileUploadFeignApi.singleUpload(bizType,file);
    }

    @ApiOperation(value = "多个文件上传", notes = "多个文件上传")
    @Override
    public BaseRestResponse multiUpload(@RequestParam("bizType") String bizType
            , @RequestParam("files") MultipartFile[] files) {
        log.info("FileUploadCommonController.multiUpload ...");
        return msFileUploadFeignApi.multiUpload(bizType,files);
    }

}

 

Spring Boot 提供了一种方便的方式来处理跨服务生成文件,通常是通过整合 RESTful API 或者消息队列来进行。以下是一个基本流程: 1. **创建API接口**:在一个微服务中,你可以创建一个RESTful API,比如POST请求,用于接收需要生成文件的数据。 ```java @RestController @RequestMapping("/file-generator") public class FileGeneratorController { @PostMapping("/generate") public ResponseEntity<ultipartFile> generateFile(@RequestBody DataModel data) { // 业务逻辑生成文件并将其封装到MultipartFile对象 MultipartFile file = ...; return ResponseEntity.ok(file); } } ``` 2. **数据传输模型**:定义一个DataModel类,包含必要的信息以便其他服务调用此接口。 ```java public class DataModel { private String fileName; private byte[] content; // 文件内容 // getters and setters } ``` 3. **调用服务并生成文件**:在另一个需要生成文件的服务中,你可以使用RestTemplate或者Feign Client等工具来调用这个API,并处理返回的MultipartFile。 ```java @Service public class AnotherService { @Autowired private RestTemplate restTemplate; public void generateFileFromOtherService(DataModel data) { ResponseEntity<ultipartFile> response = restTemplate.postForEntity( "http://other-service/file-generator/generate", data, MultipartFile.class); if (response.getStatusCode().is2xxSuccessful()) { MultipartFile file = response.getBody(); // 存储、上传或进一步处理文件 } else { handleFailure(response); } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值