FeignClient接口支持文件上传和普通参数

http://www.cppcns.com/ruanjian/java/465020.html  这个最重要,在自己构造MultipartFile对象时要注意MockMultipartFile第一个参数名必须要根接口中的参数名一致
https://www.python100.com/html/119254.html
https://blog.csdn.net/wdkymys/article/details/129643794
https://api.dandelioncloud.cn/article/details/1532938198221352961
https://blog.csdn.net/cym0359/article/details/106406302/


public class FileUtils {

    public static MultipartFile getMultipartFile(File file, String name) {
        FileInputStream fileInputStream = null;
        MultipartFile multipartFile = null;
        try {
            fileInputStream = new FileInputStream(file);
            multipartFile = new MockMultipartFile(name, file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        } catch (Exception e) {
            throw new RuntimeException("转换异常");
        }
        return multipartFile;
    }
}

Map sendRequest = new HashMap<>();
sendRequest.put("msgType", "1");
sendRequest.put("msgConfigCode", msgConfigCode);


// 构造文件对象
MultipartFile multipartFile = FileUtils.getMultipartFile(file, "file");

// 调用接口
pushFacade.sendMsg(sendRequest, multipartFile);


// 接口定义
@FeignClient("${rpc.url.timipserv}/push")
public interface PushFacade {
   
    @PostMapping(
        value = {"/msg"},
        consumes = {"multipart/form-data"}
    )
    ResponseDto sendMsg(@SpringQueryMap Map sendMsgDto, @RequestPart("file") MultipartFile file);
}



// Controller方法
 @PostMapping(value = "/msg",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "发送请求", notes = "发送请求")
    public ResponseDto sendMsg(@SpringQueryMap Map sendMsgDto, @RequestPart("file") MultipartFile file) {
        return null;
    }

你可能感兴趣的:(java)