方槍槍的java世界

          不要因為風雨飄落就停止了你的腳步,真正的得失就在你的心中。 做喜歡做的事,不輕言放棄!

          00 一個簡單的Http客戶端_SimpleHttpClient(URL)

          package com.tianhe.frm.http.client;

          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.net.URL;
          import java.net.URLConnection;

          import org.apache.log4j.Logger;

          import com.tianhe.frm.utils.LogUtil;
          import com.tianhe.frm.utils.StringUtil;

          /**
           *d:
           *
           *cd\
           *
           *java com.tianhe.frm.http.SimpleHttpClientURL http://bizhi.zhuoku.com/2012/04/21/shenlin/Shenlin01.jpg d:/wallpaper/
           * proxycn.xxx.com 8080
           *
           *java com.tianhe.frm.http.SimpleHttpClientURL http://bizhi.zhuoku.com/2012/04/21/shenlin/Shenlin01.jpg d:/wallpaper/
           * proxycn.xxx.com 8080
           */
          public class SimpleHttpClient implements Runnable
          {
              private String proxyHost = "";
             
              private String proxyPort = "80";
             
              private String srcUrl = "http://www.aygfsteel.com/Images/adminlogo.gif";
             
              private String dstDir = "d:/wallpaper";
             
              public SimpleHttpClient(String proxyHost, String proxyPort, String srcUrl, String dstDir)
              {
                  if (StringUtil.isNotEmpty(proxyHost))
                  {
                      this.proxyHost = proxyHost;
                  }
                 
                  if (StringUtil.isNotEmpty(proxyPort))
                  {
                      this.proxyPort = proxyPort;
                  }
                 
                  this.srcUrl = srcUrl;
                  this.dstDir = dstDir;
              }
             
              public SimpleHttpClient(String srcUrl, String dstDir)
              {
                  this.srcUrl = srcUrl;
                  this.dstDir = dstDir;
              }
             
              public void run()
              {
                  long beginTime = System.currentTimeMillis();
                 
                  URLConnection conn = null;
                  InputStream is = null;
                 
                  int resSize = 0;
                 
                  if (StringUtil.isNotEmpty(proxyHost))
                  {
                      System.setProperty("http.proxySet", "true");
                      System.setProperty("http.proxyHost", proxyHost);
                      System.setProperty("http.proxyPort", proxyPort);
                  }
                 
                  // ////////////////////////////////////////////////////////////////////////
                  try
                  {
                      URL url = new URL(srcUrl);
                      conn = url.openConnection();
                      is = conn.getInputStream();
                     
                      // ////////////////////////////////////////////////////////////////////////
                      resSize = processResponse(is);
                  }
                  catch (IOException e)
                  {
                      throw new RuntimeException("SimpleHttpClient.run[" + srcUrl + "] failed", e);
                  }
                 
                  long used = System.currentTimeMillis() - beginTime;
                  System.out.println(srcUrl + " succeed, used=" + used + ", resSize=" + resSize);
              }
             
              private int processResponse(InputStream is) throws IOException, FileNotFoundException
              {
                  int resSize = 0;
                  FileOutputStream fos;
                 
                  // String []df = getDirAndFile(resource);
                  // File file = createTargetFile(df[0], df[1]);
                  File file = createTargetFile("a/b", "test.gif");
                 
                  int len = -1;
                  byte[] buff = new byte[8192];
                  fos = new FileOutputStream(file);
                  while ((len = is.read(buff)) != -1)
                  {
                      fos.write(buff, 0, len);
                      resSize = resSize + len;
                  }
                 
                  fos.flush();
                  fos.close();
                  is.close();
                 
                  return resSize;
              }
             
              public File createTargetFile(String relativeDir, String resName) throws IOException
              {
                  String dir;
                 
                  if (dstDir.endsWith("/"))
                  {
                      dir = dstDir + relativeDir;
                  }
                  else
                  {
                      dir = dstDir + "/" + relativeDir;
                  }
                 
                  File fileDir = new File(dir);
                  if (!fileDir.exists())
                  {
                      fileDir.mkdirs();
                  }
                 
                  String pathFile = dir + "/" + resName;
                  File file = new File(pathFile);
                  if (!file.exists())
                  {
                      file.createNewFile();
                  }
                 
                  return file;
              }
             
              private Logger getLog()
              {
                  return LogUtil.getLog(getClass());
              }
             
              public static void usage()
              {
                  System.out.println("Usage: java SimpleHttpClient -url http://www.aygfsteel.com/Images/adminlogo.gif -dir d:/wallpaper");
              }
             
              // private String[] getDirAndFile(String resource)
              // {
              // String[] df = new String[2];
              //       
              // String vsTemp = resource;
              // int index = resource.indexOf("?");
              // if (index > 0)
              // {
              // vsTemp = vsTemp.substring(0, index);
              // int index2 = vsTemp.lastIndexOf(".");
              // vsTemp = vsTemp.substring(index2 + 1);
              // }
              // else
              // {
              // int index2 = vsTemp.lastIndexOf(".");
              // vsTemp = vsTemp.substring(index2 + 1);
              // }
              //       
              // return df;
              // }
             
              public static void main(String[] args)
              {
                  try
                  {
                      String srcUrl = "http://www.aygfsteel.com/Images/adminlogo.gif";
                      String dstDir = "d:/wallpaper";
                     
                      if (args != null && args.length > 0)
                      {
                          for (int i = 0; i < args.length; i++)
                          {
                             
                              if (args[i].startsWith("-url"))
                              {
                                  srcUrl = args[i + 1];
                                  i++;
                              }
                             
                              if (args[i].startsWith("-dir"))
                              {
                                  dstDir = args[i + 1];
                                  i++;
                              }
                             
                              if (args[i].equalsIgnoreCase("-help"))
                              {
                                  usage();
                                  System.exit(0);
                              }
                          }
                      }
                     
                      SimpleHttpClient client = new SimpleHttpClient(srcUrl, dstDir);
                      new Thread(client).start();
                  }
                  catch (Exception e)
                  {
                      e.printStackTrace();
                  }
              }
             
          }

          posted on 2012-07-09 23:03 做強大的自己 閱讀(332) 評論(0)  編輯  收藏 所屬分類: Socket

          主站蜘蛛池模板: 曲麻莱县| 汶上县| 上思县| 五家渠市| 潮州市| 治县。| 杭州市| 绥中县| 越西县| 达拉特旗| 龙州县| 丁青县| 巴青县| 兰州市| 贡觉县| 田东县| 潮州市| 嘉鱼县| 大邑县| 马鞍山市| 金山区| 萝北县| 西盟| 民勤县| 聂荣县| 汉寿县| 容城县| 凉山| 琼结县| 桦甸市| 宜州市| 右玉县| 宁远县| 宜兰县| 葫芦岛市| 富宁县| 科技| 南城县| 南郑县| 玉龙| 景泰县|