java http下载文件



    //·µ»Ø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;
    }




    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值