restTemplate 發送http post請求帶有文件流、參數


String httpMethod = "";
RestTemplate restTemplate = new RestTemplate(); String args = ""; MultiValueMap<String, Object> 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM