Beispiel #1
0
 /**
  * 获取网络图片
  *
  * @param url
  * @return
  */
 public static Bitmap getBitmapByNet(String url) throws AppException {
   // System.out.println("image_url==> "+url);
   URI uri = null;
   try {
     uri = new URI(url, false, "UTF-8");
   } catch (URIException e) {
     e.printStackTrace();
   }
   if (uri != null) url = uri.toString();
   HttpClient httpClient = null;
   GetMethod httpGet = null;
   Bitmap bitmap = null;
   int time = 0;
   do {
     try {
       httpClient = HttpHelper.getHttpClient();
       httpGet = HttpHelper.getHttpGet(url, HttpHelper.getUserAgent());
       int statusCode = httpClient.executeMethod(httpGet);
       if (statusCode != HttpStatus.SC_OK) {
         throw AppException.http(statusCode);
       }
       InputStream inStream = httpGet.getResponseBodyAsStream();
       bitmap = BitmapFactory.decodeStream(inStream);
       inStream.close();
       break;
     } catch (HttpException e) {
       time++;
       if (time < RETRY_TIME) {
         try {
           Thread.sleep(1000);
         } catch (InterruptedException e1) {
         }
         continue;
       }
       // 发生致命的异常,可能是协议不对或者返回的内容有问题
       e.printStackTrace();
       throw AppException.http(e);
     } catch (IOException e) {
       time++;
       if (time < RETRY_TIME) {
         try {
           Thread.sleep(1000);
         } catch (InterruptedException e1) {
         }
         continue;
       }
       // 发生网络异常
       e.printStackTrace();
       throw AppException.network(e);
     } finally {
       // 释放连接
       httpGet.releaseConnection();
     }
   } while (time < RETRY_TIME);
   return bitmap;
 }
  /** @param args program arguments */
  public static void main(String[] args) {
    AggressiveUrlCanonicalizer canonicalizer = new AggressiveUrlCanonicalizer();
    int n = 0;
    int i = 0;
    ArrayList<Integer> columns = new ArrayList<Integer>();

    long lineNumber = 0;
    boolean cdxPassThru = false;
    String delimiter = " ";
    while (n < args.length) {
      String arg = args[n];
      if (arg.compareTo("-cdx") == 0) {
        cdxPassThru = true;
        n++;
        continue;
      }
      if (n == (args.length - 1)) {
        USAGE();
      }
      String val = args[n + 1];
      if (arg.compareTo("-f") == 0) {
        columns.add(new Integer(val));
      } else if (arg.compareTo("-d") == 0) {
        delimiter = val;
      } else {
        USAGE();
      }
      n += 2;
    }
    // place default '0' in case none specified:
    if (columns.size() == 0) {
      columns.add(new Integer(1));
    }

    // convert to int[]:
    int[] cols = new int[columns.size()];
    for (int idx = 0; idx < columns.size(); idx++) {
      cols[idx] = columns.get(idx).intValue() - 1;
    }
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in, ByteOp.UTF8));
    StringBuilder sb = new StringBuilder();
    String line = null;

    while (true) {
      try {
        line = r.readLine();
      } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
      }
      if (line == null) {
        break;
      }
      lineNumber++;
      if (cdxPassThru && line.startsWith(CDX_PREFIX)) {
        System.out.println(line);
        continue;
      }
      String parts[] = line.split(delimiter);
      for (int column : cols) {
        if (column >= parts.length) {
          System.err.println("Invalid line " + lineNumber + " (" + line + ") skipped");
        } else {
          try {
            parts[column] = canonicalizer.urlStringToKey(parts[column]);
          } catch (URIException e) {
            System.err.println(
                "Invalid URL in line "
                    + lineNumber
                    + " ("
                    + line
                    + ") skipped ("
                    + parts[column]
                    + ")");
            e.printStackTrace();
            continue;
          } catch (StringIndexOutOfBoundsException e) {
            System.err.println(
                "Invalid URL in line "
                    + lineNumber
                    + " ("
                    + line
                    + ") skipped ("
                    + parts[column]
                    + ")");
            e.printStackTrace();
            continue;
          }
        }
      }
      sb.setLength(0);
      for (i = 0; i < parts.length; i++) {
        sb.append(parts[i]);
        if (i < (parts.length - 1)) {
          sb.append(delimiter);
        }
      }
      System.out.println(sb.toString());
    }
  }