public static String postParams(String url, Map<String, String> params, List<File> listFile) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) {
String name = (String) iter.next();
String value = String.valueOf(params.get(name));
multipartEntityBuilder.addTextBody(name, value, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
}
for (File file : listFile) {
multipartEntityBuilder.addPart("fileStream", new FileBody(file));
}
HttpEntity sentity = multipartEntityBuilder.build();
httpPost.setEntity(sentity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String entityStr = EntityUtils.toString(entity, "UTF-8");
httpPost.releaseConnection();
return entityStr;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}