首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

关于下载文件的一个迷惑

2013-12-13 
关于下载文件的一个疑惑android中通过 HttpURLConnection下载文件,下载完成得到的apk文件大小与服务器上的

关于下载文件的一个疑惑
android中通过 HttpURLConnection下载文件,下载完成得到的apk文件大小与服务器上的文件完全一致,但得到的apk文件跟服务器文件不一样,安装时提示是非法的apk,经验证确实是不合法的apk,不过纳闷儿的是大小完全一致啊,难道是在下载的过程中哪里出了细微的差异?(总共就剩45分了,,,,)
protected SizeData doInBackground(String... params) {
Log.e(TAG, "doInBackground");
int totalLength = urlConn.getContentLength();
onStart(Math.max(0, totalLength), startLocation);// 发出通知,已经准备开始

SizeData result = null;
int downloadSize = 0;
try {
inputStream = urlConn.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
result = SizeData.createErrorData(downloadSize);
}

if (inputStream != null) {
try {
outputStream = getFileStream(tempPath);
} catch (IOException e1) {
e1.printStackTrace();
result = SizeData.createErrorData(downloadSize);
}

byte[] bufByte = new byte[128];
int receiveCount = noticeBitSize / 128;
int curtCount = 0;
try {
while (alive) {
curtCount++;
int count = inputStream.read(bufByte);
if (count == -1) {
Log.e(TAG, tempPath + "下载完成");
onReceiveSuccess(downloadSize);
curtCount = 0;
result = SizeData.createRightData(downloadSize);
break;
}

if (count == 0) {
result = SizeData.createErrorData(downloadSize);
break;
}

outputStream.write(bufByte, 0, count);
downloadSize += count;
if (curtCount >= receiveCount) {
onReceiveSuccess(downloadSize);
curtCount = 0;
}
}
} catch (IOException e) {
e.printStackTrace();
result = SizeData.createErrorData(downloadSize);
}

if (!alive) {
result = SizeData.createOtherData(downloadSize);
}

try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return result;
}
[解决办法]
可以在服务器上保存文件MD5码,下载文件完成后在终端计算MD5与服务端的文件MD5进行比对来判断是否下载文件完成。关于你的下载文件代码没有细看,GOOGLE一下很多现成代码

热点排行