小弟刚学android不是太久,也不太系统,有很多东西还是不清楚的,前几天弄了个文件上传就很纠结…………想加一个进度条,但是因为文件大,不能用http协议上传,就想到了很简洁的post上传,现在倒是传上去了,但是因为Entity封装的问题,没有办法得到实时进度,想请各位大虾帮忙,我感激不尽以下是代码: protected void uploadPic(String filepath) {
try {
File file = new File(filepath); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
HttpPost request = new HttpPost("http://192.168.1.101:8086/MyService/myfile");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpClient hc = new DefaultHttpClient(params);
HttpResponse hrs = null;
InputStreamEntity instreamEntity = new InputStreamEntity(new FileInputStream(file), file.length());
request.setEntity(instreamEntity);
hrs = hc.execute(request);
System.out.println(instreamEntity.getContentLength());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在此先感谢各位了
try {
File file = new File(filepath); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
HttpPost request = new HttpPost("http://192.168.1.101:8086/MyService/myfile");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpClient hc = new DefaultHttpClient(params);
HttpResponse hrs = null;
InputStreamEntity instreamEntity = new InputStreamEntity(new FileInputStream(file), file.length());
request.setEntity(instreamEntity);
hrs = hc.execute(request);
System.out.println(instreamEntity.getContentLength());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在此先感谢各位了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货