1、请求目标接口
url:https://xxxx/
header/params:
paramA | 必填String,例:a |
Body:form-data
param | 必填String,例:aaaa |
结果:返回文件的流
public class Demo {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://xxxx/?paramA=a";
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("param","aaaa");
ResponseEntity<Resource> entity = restTemplate.postForEntity(url, paramMap, Resource.class);
try {
InputStream input = Objects.requireNonNull(entity.getBody()).getInputStream();
File file = new File("d:/test.docx");
copyInputStreamToFile(input,file);
input.close();
} catch (Exception e){
}
}
/**
* 流输出到文件的方法
*/
private static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
}
}