java http下載檔案
阿新 • • 發佈:2019-01-01
//·µ»Ø0ÏÂÔسɹ¦ ¸ºÊýÏÂÔØʧ°Ü
public int DownloadDBAndCheck(String downURL, String savePath, String hash, boolean check_hash) {
RandomAccessFile saveFile = null;
HttpURLConnection httpConnection = null;
InputStream input = null;
try {
LogUtils.printLogs(TAG, "DownloadDBAndCheck, download start");
URL url = null;
long nPos = 0;
String strDownURL = downURL;
long fileLength = 0, dataLen = 0;
if( MainActivity.isProduceVersion ){
strDownURL = ClientXdnUpdate.getCustomBase64URL(downURL);
}else {
Long rand = HttpUtils.get_long_random_num();
strDownURL = strDownURL + "?" + String.valueOf(rand);
//ÓÃ?¼ÓËæ»úÊý,±£Ö¤²»ÊÇ»º´æ
}
LogUtils.printLogs( TAG, "strDownURL = " + strDownURL );
Message msg;
msg = new Message();
msg.what = MSG_HANDLER.msgDownloadChildProgress;
Bundle bundle = new Bundle();
bundle.putLong("progress", 0);
msg.setData(bundle);
downloadHandler.sendMessage(msg);
url = new URL(strDownURL);
httpConnection = (HttpURLConnection) url.openConnection();
if( httpConnection == null ){
LogUtils.printLogs(TAG, "DownloadDBAndCheck, httpConnection == null");
Thread.sleep(500);
return -1;
}
httpConnection.setConnectTimeout(3000);
httpConnection.setReadTimeout(3000);
dataLen = fileLength = httpConnection.getContentLength();
if( fileLength < 0 ){
return -2;
}
if( !(httpConnection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL ||
httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) ){
LogUtils.printLogs( TAG, "HTTP ERROR CODE:" + httpConnection.getResponseCode() );
return -3;
}
input = httpConnection.getInputStream();
if( input == null ){
LogUtils.printLogs( TAG, "download file:" + "input error");
return -4;
}
FileExt.deleteFile(savePath);
saveFile = new RandomAccessFile(savePath, "rw");
if( saveFile == null ){
LogUtils.printLogs( TAG, "download file:" + "RandomAccessFile error" );
return -5;
}
//¶¨Î»ÎļþÖ¸Õëµ½ nPos λÖÃ
saveFile.seek(nPos);
byte[] b = new byte[1024*1024];
int readLen;
long buffLen = nPos;
//´ÓÊäÈëÁ÷ÖжÁÈë×Ö½ÚÁ÷,È»ºóдµ½ÎļþÖÐ
childProgressBar.setMax( (int)fileLength );
//½ø¶ÈÌõ´¦Àí
LogUtils.printLogs( TAG, "download file's len = " + fileLength );
while( true ){
readLen = input.read( b, 0, 1024*1024 );
if( readLen > 0 ){
saveFile.write(b, 0, readLen);
buffLen = buffLen + readLen;
//LogUtils.printLogs(TAG, "download file:" + readLen);
msg = new Message();
msg.what = MSG_HANDLER.msgDownloadChildProgress;
bundle = new Bundle();
bundle.putLong("progress", buffLen);
msg.setData(bundle);
downloadHandler.sendMessage(msg);
//¸üнø¶ÈÌõ
if( buffLen > fileLength ){
//³ö´í
LogUtils.printLogs(TAG, "download file:" + "download error");
FileExt.deleteFile(savePath);
return -6;
}else{
if( buffLen == fileLength ){
LogUtils.printLogs(TAG, "fileLength:" + fileLength);
LogUtils.printLogs(TAG, "buffLen:" + buffLen);
break;
}
}
}else if( readLen <= 0 ){
LogUtils.printLogs(TAG, "readLen:" + readLen);
LogUtils.printLogs(TAG, "download finish0");
FileExt.deleteFile(savePath);
return -7;
}
}
} catch (Exception e) {
//e.printStackTrace();
FileExt.deleteFile(savePath);
return -8;
} finally {
try {
if( input != null ){
input.close();
}
if( httpConnection != null ){
httpConnection.disconnect();
}
if( saveFile != null ){
saveFile.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return 0x00;
}