MultipartFile类参数接口调用示例

版权声明:本文为博主原创文章,转载时请在文章最前方附上本文地址。 https://blog.csdn.net/qq_35033270/article/details/85344911

Http请求时,遇接口有MultipartFile类型参数时,可按如下解决:

 //filePath,即文件路径
 FileSystemResource resource = new FileSystemResource(new File(filePath));
 MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
 // 此files对应请求接口方法参数变量,需一致,否则对方接口接不到参数
 param.add("files", resource);
// 根据restTemplate来发送请求
Object object = restTemplate.postForObject(serverUrl
							, param, Object.class);

ps,多提一句RestTemplate的用法,可加如下配置类

 @Configuration
 public class ServiceConfig {
/**
 * RestTemplate
 */
@Bean
public RestTemplate restTemplate() {
	return new RestTemplate();
    }
 }

然后在需要用的类中,自动注入,在类中调用即可:

@Autowired
private RestTemplate restTemplate;

猜你喜欢

转载自blog.csdn.net/qq_35033270/article/details/85344911
今日推荐