问题描述
我试图下载使用HttpClient的4,它会在大约.5 zip文件(千字节/千位)?每分钟。该文件是小于一个MB的大,并且下载可能会需要一个小时!难道我做错了什么?我应该怎么做别的呢?这是我目前的执行情况:
@覆盖
保护乌里doInBackground(字符串... PARAMS){
publishProgress(下载...);
尝试 {
HttpPost searchPOST =新HttpPost(http://www.somesite.com/+ searchResult.getURLSuffix());
名单<的NameValuePair> formparams =新的ArrayList<的NameValuePair>();
//这里添加的参数...
UrlEn codedFormEntity paramsEntity =新UrlEn codedFormEntity(formparams,HTTP.UTF_8);
searchPOST.setEntity(paramsEntity);
HTT presponse manualResponse = client.execute(searchPOST);
头fileNameHeader = manualResponse.getFirstHeader(内容处置);
模式p = Pattern.compile(文件名= \(+)\。);
匹配器米= p.matcher(fileNameHeader.getValue());
如果(m.find()){
字符串文件名= m.group(1);
InputStream的zipStream = manualResponse.getEntity()的getContent()。
文件cacheDir = context.getCacheDir();
串tempFileForZip = cacheDir.getAbsolutePath()+/+文件名;
FileOutputStream中FOS =新的FileOutputStream(tempFileForZip);
INT bytesDownloaded = 0;
尝试 {
INT℃;
而((C = zipStream.read())!= - 1){
fos.write(C);
bytesDownloaded ++;
kilobytesDownloaded =(bytesDownloaded / 1000);
publishProgress((字符串[])NULL);
}
} 最后 {
如果(zipStream!= NULL){
zipStream.close();
}
如果(FOS!= NULL){
fos.close();
}
}
fos.close();
字符串zipFilePath = tempFileForZip;
//更改为不确定
kilobytesDownloaded =文件大小;
publishProgress(提取...);
// TODO:preferences的保存目录
saveDirectory =新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()+/+下载/);
ZipTools.unzipArchive(新文件(zipFilePath),saveDirectory);
}
}赶上(IllegalStateException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
} 最后 {
}
返回Uri.fromFile(saveDirectory);
}
步骤#1:不要叫 publishProgress()
为每个字节
步骤#2:了解更多比一个字节的时间。更重要的是,不要使用的InputStream
直接 - 使用 HttpEntity#的writeTo()
有的HttpClient
您的数据写入到输出文件。
I am trying to download a zip file using HttpCLient 4 and it is going at around .5 (kilobytes/kilobits)? per minute. The file is less than a MB large, and the download will probably take an hour! Am I doing something wrong? How else should I do this? Here is my current implementation:
@Override
protected Uri doInBackground(String... params) {
publishProgress("Downloading...");
try {
HttpPost searchPOST = new HttpPost("http://www.somesite.com/" + searchResult.getURLSuffix());
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//added parameters here...
UrlEncodedFormEntity paramsEntity = new UrlEncodedFormEntity(formparams, HTTP.UTF_8);
searchPOST.setEntity(paramsEntity);
HttpResponse manualResponse = client.execute(searchPOST);
Header fileNameHeader = manualResponse.getFirstHeader("Content-Disposition");
Pattern p = Pattern.compile("filename=\"(.+?)\"");
Matcher m = p.matcher(fileNameHeader.getValue());
if (m.find()) {
String fileName = m.group(1);
InputStream zipStream = manualResponse.getEntity().getContent();
File cacheDir = context.getCacheDir();
String tempFileForZip = cacheDir.getAbsolutePath() + "/" + fileName;
FileOutputStream fos = new FileOutputStream(tempFileForZip);
int bytesDownloaded = 0;
try {
int c;
while ((c = zipStream.read()) != -1) {
fos.write(c);
bytesDownloaded++;
kilobytesDownloaded=(bytesDownloaded / 1000);
publishProgress((String[])null);
}
} finally {
if (zipStream != null) {
zipStream.close();
}
if (fos != null) {
fos.close();
}
}
fos.close();
String zipFilePath = tempFileForZip;
//Change to indeterminate
kilobytesDownloaded = fileSize;
publishProgress("Extracting...");
//TODO: Preferences for save directory
saveDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Downloads/");
ZipTools.unzipArchive(new File(zipFilePath), saveDirectory);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
return Uri.fromFile(saveDirectory);
}
Step #1: Do not call publishProgress()
for every byte.
Step #2: Read more than a byte at a time. Better yet, don't use the InputStream
directly -- use HttpEntity#writeTo()
to have HttpClient
write your data to the output file.
这篇关于Apache的HttpClient的Android中极其缓慢的下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!