方枪枪的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.blogjava.net/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.blogjava.net/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.blogjava.net/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 做强大的自己 阅读(329) 评论(0)  编辑  收藏 所属分类: Socket


只有注册用户登录后才能发表评论。


网站导航: