您的位置:首页 > 其它

POST以流的方式传送文件

2015-08-14 09:14 225 查看
public static void postUpload(String json, String pwd,String targetFile) throws Exception{
HttpClient httpclient = new DefaultHttpClient();
//请求处理页面
HttpPost httppost = new HttpPost(
"http://10.2.5.162:8081/Default.aspx");
//创建待处理的文件
FileBody file = new FileBody(new File(targetFile));
//创建待处理的表单域内容文本
StringBody descript = new StringBody(json);
StringBody des= new StringBody(pwd);
//对请求的表单域进行填充
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", file);
reqEntity.addPart("json", descript);
reqEntity.addPart("password", des);
//设置请求
httppost.setEntity(reqEntity);
//执行
HttpResponse response = httpclient.execute(httppost);
//HttpEntity resEntity = response.getEntity();
//System.out.println(response.getStatusLine());
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity();
//显示内容
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
if (entity != null) {
entity.consumeContent();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: