예제 #1
0
  public void getNetData(int readTimeout, int connTimeout, BdHttpStat stat) throws Exception {
    stat.executeStatus = ExecuteStatus.BEGIN;
    if (context.getResponse().isCancel == true) {
      throw new BdHttpCancelException();
    }
    String urlString = context.getRequest().generateGetString(stat);

    URL url = new URL(urlString);
    if (context.getResponse().isCancel == true) {
      throw new BdHttpCancelException();
    }
    stat.executeStatus = ExecuteStatus.CREATE_CONN_BEFORE;
    HttpURLConnection conn = getConnect(url);
    stat.executeStatus = ExecuteStatus.CREATE_CONN_SUCC;
    long time = System.currentTimeMillis();
    try {
      if (mConn == null) {
        throw new java.net.SocketException("network not available.");
      }
      mConn.setRequestMethod("GET");
      mConn.setConnectTimeout(connTimeout);
      mConn.setReadTimeout(readTimeout);
      context.getRequest().wrapHead(conn);
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }
      // 拖慢网络速度,暂时去掉
      // checkDNS(url);
      stat.dnsTime = new Date().getTime() - time;
      BdLog.i("GET:" + urlString);
      stat.executeStatus = ExecuteStatus.CONN_BEFORE;
      conn.connect();
      stat.executeStatus = ExecuteStatus.CONN_SUCC;
      stat.connectTime = new Date().getTime() - time - stat.dnsTime;
      if (context.getResponse().isCancel == true) {
        throw new BdHttpCancelException();
      }

      stat.executeStatus = ExecuteStatus.GETDATA_BEFORE;
      context.getResponse().getResponseHead(mConn);
      stat.responsedCode = context.getResponse().responseCode;
      context.getResponse().retBytes = getResponse(mConn);

      if (context.getResponse().retBytes != null) {
        stat.downloadSize = context.getResponse().retBytes.length;
      }
      stat.executeStatus = ExecuteStatus.GETDATA_SUCC;
      stat.rspTime = new Date().getTime() - time;
    } finally {
      if (mConn != null) {
        mConn.disconnect();
      }
    }
  }
예제 #2
0
  /**
   * 下载文件
   *
   * @return true:成功; false:失败
   */
  @SuppressWarnings("resource")
  public boolean downloadFile(
      String name, Handler handler, int what, int readTimeout, int conntimeout) throws Exception {
    InputStream in = null;
    boolean ret = false;
    long time = 0;
    FileOutputStream fileStream = null;
    try {
      URL url = new URL(context.getRequest().getUrl());
      mConn = getConnect(url);
      if (mConn == null) {
        throw new java.net.SocketException();
      }
      mConn.setConnectTimeout(conntimeout);
      mConn.setReadTimeout(readTimeout);
      mConn.setInstanceFollowRedirects(true);
      if (context.getResponse().isCancel == true) {
        return false;
      }
      time = new Date().getTime();

      File file = ZXFileUtil.createFileIfNotFound(name);
      if (file == null) {
        throw new FileNotFoundException();
      }
      long file_length = file.length();
      fileStream = new FileOutputStream(file, true);
      //			if (context.getRequest().mIsLimited == true) {
      //				mConn.addRequestProperty(
      //						"Range",
      //						"bytes=" + String.valueOf(file_length) + "-"
      //								+ String.valueOf(file_length + 200000));
      //			} else {
      mConn.addRequestProperty("Range", "bytes=" + String.valueOf(file_length) + "-");
      //			}

      mConn.connect();

      //            while(mConn.getResponseCode()==302){
      //                mConn.disconnect();
      //                context.getRequest().setUrl(mConn.getHeaderField("location"));
      //                mConn.connect();
      //            }

      context.getResponse().responseCode = mConn.getResponseCode();
      if (!isFileSegSuccess()) {
        throw new UnsupportedOperationException();
      }

      /** 判断是否是移动的提示信息 */
      if (mConn.getContentType().contains("text/vnd.wap.wml") == true) {
        mConn.disconnect();
        context.getResponse().responseCode = 0;
        return downloadFile(name, handler, what, readTimeout, conntimeout);
      }
      int contentLen = 0;
      String range = mConn.getHeaderField("Content-Range");
      if (range != null) {
        int index = range.indexOf("/");
        if (index != -1) {
          contentLen = toInt(range.substring(index + 1), 0);
        }
      }
      if (contentLen == 0 && context.getResponse().responseCode == HttpStatus.SC_OK) {
        String length = mConn.getHeaderField("Content-Length");
        if (length != null) {
          contentLen = toInt(length, 0);
        } else {
          length = mConn.getHeaderField("Accept-Length");
          if (length != null) {
            contentLen = toInt(length, 0);
          }
        }
      }
      contentLen = contentLen == 0 ? 1 : contentLen;
      if (file_length >= contentLen) {
        return true;
      }
      in = mConn.getInputStream();
      byte[] buf = new byte[BUFFERSIZE];
      int num = -1;
      int datalenth = 0;
      int notify_num = 0;
      if (contentLen > 0) {
        notify_num = contentLen / 50;
      }
      int notify_tmp = 0;
      if (handler != null && file_length > 0) {
        handler.sendMessage(handler.obtainMessage(what, (int) file_length, contentLen));
      }
      while (context.getResponse().isCancel == false && (num = in.read(buf)) != -1) {
        try {
          fileStream.write(buf, 0, num);
        } catch (Exception ex) {
          throw new FileNotFoundException();
        }
        datalenth += num;
        notify_tmp += num;
        if (handler != null && (notify_tmp > notify_num || datalenth == contentLen)) {
          notify_tmp = 0;
          handler.sendMessage(
              handler.obtainMessage(what, (int) (datalenth + file_length), contentLen));
        }
      }
      try {
        fileStream.flush();
      } catch (Exception ex) {
        throw new FileNotFoundException();
      }
      time = new Date().getTime() - time;
      BdLog.i("NetWork", "downloadFile", "time = " + String.valueOf(time) + "ms");
      if (contentLen != -1) {
        BdLog.i("NetWork", "downloadFile", "data.zise = " + String.valueOf(contentLen));
      }
      if (datalenth + file_length >= contentLen) {
        ret = true;
      }
    } finally {
      BdCloseHelper.close(in);
      BdCloseHelper.close(mConn);
      BdCloseHelper.close(fileStream);
    }
    return ret;
  }