public static void upload(String url, List<String> filepaths,HashMap<String, String> mapParams) {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "utf-8");
try {
MultipartEntity entity = new MultipartEntity();//多个表单对象
for(String filepath:filepaths){
ContentBody fileBody = new FileBody(new File(filepath)); //表单文件域
entity.addPart("file", fileBody);
}
entity.addPart("userName", new StringBody(mapParams.get("userName"))); // 字符参数部分
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);//执行post操作,并返回response
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String jsonData = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("response result="+jsonData);
}else{
System.out.println("no response");
}
} catch (Exception e) {
e.printStackTrace();
}
}
HttpClient发送方实现上传多个文件
最新推荐文章于 2024-02-22 18:39:55 发布