我们正在尝试从服务器下载近7MB的apk文件。在从输入流中读取数据时,流程将终止。我们没有收到任何错误消息。以下是我们厌倦的代码。
URL url = new URL(versionUpgradeModel.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setConnectTimeout(3 * 60 * 1000);
connection.connect();
long totalBytes = 0;
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
totalBytes = connection.getContentLength();
}
String filePath = localFilePath + "/TestApp.apk";
CWTSLog.e("FILE", "CREATED" + localFilePath);
OutputStream outputStream = null;
try
{
// write the inputStream to a FileOutputStream
outputStream = new FileOutputStream(filePath);
int bufferszie = 4096;
int read = 0;
double consumedBytes = 0;
double progress = 0;
byte[] bytes = new byte[bufferszie];
CWTSLog.e("TAG", "LENGTH " + totalBytes);
while ((read = inputStream.read(bytes)) != -1) {
CWTSLog.e("Read: ", "" + read);
outputStream.write(bytes, 0, read);
consumedBytes += read;
CWTSLog.e("CONSUMED BYTE", "" + consumedBytes);
progress = (consumedBytes / totalBytes) * 100;
CWTSLog.e("PROGRESS", "" + progress);
progressBar.setProgress((int) progress);
}
}
我们试图调试,但它在7%后读取输入流时停止了。但是在浏览器中使用相同的URL时,它的下载很好。请帮助解决此问题。
答案 0 :(得分:4)
我也遇到了类似的问题,我的视频过去只下载了5Mb,然后失败了。 我的问题是当我初始化bufferedInputStream()时。 不要给大小,我评论那部分。试试吧。
使用此代码:
/**
* downloadVideoAsyntask aysnctask is used to download video from server.
*/
class DownlaodVideoAsyntask extends AsyncTask<String, String, Boolean> {
protected void onPreExecute() {
Utils.showProgressDialog(mActivity, "Downloading...", false);
isAsyntaskWorking = true;
};
protected void onProgressUpdate(String... values) {
Utils.showLog(TAG, values[0]);
};
@Override
protected Boolean doInBackground(String... params) {
final int TIMEOUT_CONNECTION = 5000;// 5sec
final int TIMEOUT_SOCKET = 30000;// 30sec
String imageURL = params[0];
Utils.showLog(TAG, imageURL);
URL url = null;
try {
url = new URL(imageURL);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long startTime = System.currentTimeMillis();
Log.i(TAG, "image download beginning: " + imageURL);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/" + AppHelper.STORED_VIDEO_PATH);
if (!myDir.exists()) {
myDir.mkdirs();
}
String fname = System.currentTimeMillis() + ".avi";
file = new File(myDir, fname);
// Open a connection to that URL.
URLConnection ucon = null;
try {
ucon = url.openConnection();
lengthofFile = ucon.getContentLength();
Utils.showLog("ANDRO_ASYNC", "Length of file: " + lengthofFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// this timeout affects how long it takes for the app to realize
// there's
// a connection problem
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);
// Define InputStreams to read from the URLConnection.
InputStream is = null;
try {
is = ucon.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream inStream = new BufferedInputStream(is);
// new BufferedInputStream(is, 1024 * 18);
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buff = new byte[5 * 1024];
long total = 0;
// Read bytes (and store them) until there is nothing more to
// read(-1)
int len;
try {
while ((len = inStream.read(buff)) != -1) {
total += len;
publishProgress("" + (int) ((total * 100) / lengthofFile));
outStream.write(buff, 0, len);
}
// clean up
outStream.flush();
inStream.close();
outStream.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "download completed in "
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
return false;
}
protected void onPostExecute(Boolean result) {
Utils.hideProgressDialog();
isAsyntaskWorking = false;
if (result) {
// if successfully download the video.
Utils.showToast(mActivity,
getResources().getString(R.string.toast_video_success));
} else {
Utils.showToast(mActivity,
getResources().getString(R.string.toast_video_fail));
// deleting partially downloaded file.
file.delete();
}
};
}