这里是下载功能,我可以使用监听器来获取下载进度,如何像下载功能一样获取上传进度。谢谢!
public void doGetToFile(String url, String localFilePath,CloudStatusListener listener) throws RestHttpException, HttpException {
final HttpGet request = new HttpGet(url);
final HttpResponse resp;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
BasicCredentialsProvider creds= new BasicCredentialsProvider();
creds.setCredentials(new AuthScope(CloudClient.CLOUDHOST,CloudClient.CLOUDPORT),new UsernamePasswordCredentials(UserName,Password));
httpClient.setCredentialsProvider(creds);
resp = httpClient.execute(request);
long totalnum=resp.getEntity().getContentLength();
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
FileOutputStream out = new FileOutputStream(localFilePath);
InputStream inputStream=resp.getEntity().getContent();
long bytesRead=0;
int bufferSize=listener.progressInterval();
byte b[]=new byte[bufferSize];
int cnt=0;
while((cnt=inputStream.read(b))!=-1)
{
out.write(b,0,cnt);
bytesRead+=cnt;
listener.onProgress(bytesRead, totalnum);
}
out.flush();
out.close();
//resp.getEntity().writeTo(out);
out.close();
return;
} else {
final String response = EntityUtils.toString(resp.getEntity());
throw new RestHttpException(resp.getStatusLine().getStatusCode(), response);
}
} catch (final IOException e) {
e.printStackTrace();
throw new HttpException("IOException " + e.toString());
}
}