下图是我的所有测试接口,包含两个表单提交接口和一个Rest接口:
我是用的Http请求工具是Spring自带的RestTemplate。
请求的方法如下:
三个请求分别对应三个接口,在此记录下。
下载文件,获取文件字节流:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
ResponseEntity<byte[]> entity = restTemplate.exchange("http://ip:port/test.doc", HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
byte[] body = entity.getBody();
multipart/form-data 文件上传:
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:8080/file/upload"
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
// 设置multi/form-data文件
multiValueMap.add("file", new FileSystemResource("D:/1.mp3"));
multiValueMap.add("name", "测试材料");
// http请求
String response = restTemplate.postForObject(url, multiValueMap, String.class);