restTemplate 发送http post请求带有文件流、参数
String httpMethod = ""; RestTemplate restTemplate = new RestTemplate(); String args = ""; MultiValueMap paramMap = new LinkedMultiValueMap<>(); paramMap.add("args", args); paramMap.add("sign", ""); File file=new File("D:\\5a658c98Nd0abcb82.jpg"); byte[] bytesArray = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); fis.read(bytesArray); //read file into bytes[] fis.close(); ByteArrayResource contentsAsResource = new ByteArrayResource(bytesArray) { @Override public String getFilename() { return "img"; } }; paramMap.add("img", contentsAsResource); JSONObject json = restTemplate.postForObject(httpMethod, paramMap, JSONObject.class); System.out.println("post json : " + json);
注意点
1.返回处理
2.MultiValueMap