方槍槍的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

          主站蜘蛛池模板: 布尔津县| 囊谦县| 黄石市| 若尔盖县| 灵川县| 黎平县| 靖江市| 曲沃县| 洱源县| 延寿县| 新乡市| 九龙城区| 铜鼓县| 武定县| 行唐县| 高碑店市| 阿合奇县| 夏河县| 高唐县| 吉安县| 龙口市| 临江市| 陵川县| 祁连县| 许昌市| 滨海县| 洛扎县| 叙永县| 林甸县| 天津市| 长沙市| 博湖县| 衡山县| 阿拉善盟| 伊通| 平陆县| 郓城县| 德格县| 石楼县| 永和县| 隆化县|