public static CloseableHttpResponse putFilesWithHeader(String url,JSONObject requestJson,Map<String, String> header ,File... files) { CloseableHttpResponse execute = null; HttpPut httpPut = new HttpPut(url); String responseStr = null; if (header != null) { for (Map.Entry<String, String> entry : header.entrySet()) { httpPut.setHeader(entry.getKey(),entry.getValue()); } } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName("UTF-8")); for (File file : files) { multipartEntityBuilder.addBinaryBody("facePic", file); } Set<Map.Entry<String, Object>> entries = requestJson.entrySet(); for (Map.Entry<String, Object> requestParam : entries ) { multipartEntityBuilder.addTextBody(requestParam.getKey(),(String) requestParam.getValue()); } HttpEntity entiy = multipartEntityBuilder.build(); httpPut.setEntity(entiy); CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); try { execute = closeableHttpClient.execute(httpPut); } catch (Exception e) { e.printStackTrace(); } return execute; }
java 发送上传文件的 http put 请求
最新推荐文章于 2024-08-05 23:16:41 发布