锘??xml version="1.0" encoding="utf-8" standalone="yes"?>最新国产精品久久精品,西野翔中文久久精品国产,在线观看麻豆视频http://www.aygfsteel.com/tiandizhijian/category/48422.html涓嶈鍥犱負椋庨洦椋樿惤灝卞仠姝簡浣犵殑鑴氭錛岀湡姝g殑寰楀け灝卞湪浣犵殑蹇冧腑銆?/description>zh-cnSun, 15 Jul 2012 01:47:28 GMTSun, 15 Jul 2012 01:47:28 GMT6001 涓涓畝鍗曠殑Http瀹㈡埛绔痏SimpleHttpClient(Socket)http://www.aygfsteel.com/tiandizhijian/articles/382656.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 15:07:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382656.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382656.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382656.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382656.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382656.htmlpackage com.tianhe.ot.http.bak;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import com.tianhe.frm.http.server.Constant;
import com.tianhe.frm.utils.SocketUtil;

public class SimpleHttpClientSocket implements Runnable
{
    private ResUrlBean resUrlBean;
   
    private String resName;
   
    private String resUrl;
   
    public void setResUrlBean(ResUrlBean resUrlBean)
    {
        this.resUrlBean = resUrlBean;
    }
   
    public void setResName(String resName)
    {
        this.resName = resName;
    }
   
    public void setResUrl(String resUrl)
    {
        this.resUrl = resUrl;
    }
   
    public SimpleHttpClientSocket(ResUrlBean resUrlBean, String resUrl, String resName)
    {
        this.resUrlBean = resUrlBean;
        this.resName = resName;
        this.resUrl = resUrl;
    }
   
    public void run()
    {
        long beginTime = System.currentTimeMillis();
       
        String vsTemp;
       
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
       
        BufferedReader in = null;
       
        int resSize = 0;
       
        try
        {
            socket = createSocket(resUrlBean.getHost(), resUrlBean.getPort());
            is = socket.getInputStream();
            os = socket.getOutputStream();
           
            resUrl = "           
            // ////////////////////////////////////////////////////////////////////////
            // 閸欐垿?鐠囬攱鐪?br />            StringBuffer sb = new StringBuffer();
            appendLn(sb, "GET " + resUrl + " HTTP/1.1");
            appendLn(sb,
                    "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            appendLn(sb, "Accept-Language: zh-cn");
            appendLn(sb, "UA-CPU: x86");
            appendLn(sb, "Accept-Encoding: gzip, deflate");
            appendLn(sb,
                    "Operator-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
            appendLn(sb, "Host: " + resUrlBean.getHost());
            appendLn(sb, "Connection: Keep-Alive");
            appendLn(sb);
            os.write(sb.toString().getBytes());
            os.flush();
           
            // ////////////////////////////////////////////////////////////////////////
            // 閹恒儲鏁歸崫宥呯安
            in = new BufferedReader(new InputStreamReader(is));
            int contentLength = 0;
            String line = in.readLine();
            while ((line = in.readLine()) != null)
            {
               
                if (line.startsWith(Constant.CONTENT_LENGTH))
                {
                    vsTemp = line.substring(Constant.CONTENT_LENGTH.length());
                    contentLength = Integer.valueOf(vsTemp);
                }
               
                System.out.println(line);
               
                if (line.equals(""))
                    break;
            }
           
            // 閸愭瑥鍙嗛弬鍥︽
            if (contentLength > 0)
            {
                File file = resUrlBean.getTargetFile(resName);
                if (!file.exists())
                {
                    file.createNewFile();
                }
               
                int len = -1;
                byte[] buff = new byte[8192];
                FileOutputStream fos = new FileOutputStream(file);
                while ((len = is.read(buff)) != -1)
                {
                    fos.write(buff, 0, len);
                    resSize = resSize + len;
                }
               
                fos.flush();
                fos.close();
                os.close();
                in.close();
            }
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            SocketUtil.close(is);
            SocketUtil.close(os);
            SocketUtil.close(socket);
        }
       
        long used = System.currentTimeMillis() - beginTime;
        System.out.println(resUrl + " succeed, used=" + used + ", resSize=" + resSize);
    }
   
    private StringBuffer appendLn(StringBuffer sb, String xxx)
    {
        sb.append(xxx).append("\r\n");
        return sb;
    }
   
    private StringBuffer appendLn(StringBuffer sb)
    {
        sb.append("\r\n");
        return sb;
    }
   
    private static String getAllUrl(String[] args)
    {
        String temp = "
        if (args != null && args.length > 0)
        {
            temp = args[0];
        }
        return temp;
    }
   
    private static String getTargetDir(String[] args)
    {
        String temp = "d:/wallpaper";
        if (args != null && args.length > 1)
        {
            temp = args[1];
        }
        return temp;
    }
   
    private static String getProxyHost(String[] args)
    {
        String temp = "";
        if (args != null && args.length > 2)
        {
            temp = args[2];
        }
        return temp;
    }
   
    private static int getProxyPort(String[] args)
    {
        int temp = 80;
        if (args != null && args.length > 3)
        {
            temp = Integer.valueOf(args[3]);
        }
        return temp;
    }
   
    private static ResUrlBean createResUrlBean(String proxyHost, int proxyPort, String allUrl, String targetDir)
    {
        ResUrlBean rub = null;
        if (proxyHost != null && (!proxyHost.trim().equals("")))
        {
            // for url use
            rub = new ResUrlBean(true);
            rub.setHost(proxyHost);
            rub.setPort(proxyPort);
        }
        else
       
        {
            // for socket use, not for url use
            rub = new ResUrlBean();
            rub.setHost(getHostFromUrl(allUrl));
            rub.setPort(getPortFromUrl(allUrl));
        }
        rub.setAllUrl(allUrl);
        rub.setTargetDir(targetDir);
       
        {
            int pos1 = -1;
            int pos2 = -1;
            int pos3 = -1;
           
            String ts = "";
           
            pos1 = allUrl.indexOf("://") + 3;
            pos2 = allUrl.indexOf("/", pos1) + 1;
            pos3 = allUrl.lastIndexOf("/");
            ts = allUrl.substring(pos2, pos3);
            rub.setRelativePath(ts);
        }
       
        rub.setBaseUrl(allUrl.substring(0, allUrl.lastIndexOf("/") + 1));
        rub.setSuffix(allUrl.substring(allUrl.length() - 4));
        rub.setStartIndex(1);
       
        {
            String ts = allUrl.substring(allUrl.lastIndexOf("/") + 1, allUrl.length() - 4);
            int pos = 0;
            char[] tc = ts.toCharArray();
            for (int i = tc.length - 1; i > 0; i--)
            {
                int ti = Integer.valueOf(tc[i]);
                if (ti < 48 || ti > 57)
                {
                    pos = i + 1;
                    break;
                }
            }
            rub.setPrefix(ts.substring(0, pos));
            rub.setEndIndex(Integer.valueOf(ts.substring(pos)));
            rub.setFormat("%0" + ts.substring(pos).length() + "d");
        }
       
        System.out.println("resUrlBean=" + rub.toString());
        return rub;
    }
   
    private static String getHostFromUrl(String allUrl)
    {
        // allUrl = "
        // allUrl = "       
        int pos1 = -1;
        int pos2 = -1;
        String ts = "";
       
        pos1 = allUrl.indexOf("://") + 3;
        pos2 = allUrl.indexOf("/", pos1);
       
        ts = allUrl.substring(pos1, pos2);
        pos1 = ts.indexOf(":");
        if (pos1 > 0)
        {
            ts = ts.substring(0, pos1);
        }
       
        // System.out.println("getHostFromUrl:" + ts);
        return ts;
    }
   
    private static int getPortFromUrl(String allUrl)
    {
        // allUrl = "
        // allUrl = "       
        int port = 80;
       
        int pos1 = -1;
        int pos2 = -1;
        String ts = "";
       
        pos1 = allUrl.indexOf("://") + 3;
        pos2 = allUrl.indexOf("/", pos1);
       
        ts = allUrl.substring(pos1, pos2);
        pos1 = ts.indexOf(":");
        if (pos1 > 0)
        {
            ts = ts.substring(pos1) + 1;
            port = Integer.valueOf(ts);
        }
       
        // System.out.println("getPortFromUrl:" + port);
        return port;
    }
   
    private static String createResName(ResUrlBean rub, int i)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(rub.getPrefix());
        String format = rub.getFormat();
        if (format != null && (!format.trim().equals("")))
        {
            sb.append(String.format(format, i));
        }
        sb.append(rub.getSuffix());
        return sb.toString();
    }
   
    private static SimpleHttpClientSocket createSimpleHttpClient(ResUrlBean rub, int i)
    {
        String resName = createResName(rub, i);
        String resUrl = rub.getBaseUrl() + resName;
        SimpleHttpClientSocket client = new SimpleHttpClientSocket(rub, resUrl, resName);
        return client;
    }
   
    //
http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui01.jpg
    public static void main(String[] args)
    {
        String allUrl = getAllUrl(args);
        System.out.println("allUrl=" + allUrl);
       
        String targetDir = getTargetDir(args);
        System.out.println("targetDir=" + targetDir);
       
        String proxyHost = getProxyHost(args);
        System.out.println("proxyHost=" + proxyHost);
       
        int proxyPort = getProxyPort(args);
        System.out.println("proxyPort=" + proxyPort);
       
        ResUrlBean rub = createResUrlBean(proxyHost, proxyPort, allUrl, targetDir);
       
        try
        {
            for (int i = rub.getStartIndex(); i <= rub.getEndIndex(); i++)
            {
                SimpleHttpClientSocket client = createSimpleHttpClient(rub, i);
                new Thread(client).start();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
       
    }
   
    private Socket createSocket(String proxyHost, int proxyPort) throws Exception
    {
        Socket socket = null;
        try
        {
            socket = new Socket(proxyHost, proxyPort);
        }
        catch (Exception e)
        {
            SocketUtil.close(socket);
            throw e;
        }
       
        if (socket == null)
        {
            System.exit(1);
        }
       
        return socket;
    }
}



]]>
00 涓涓畝鍗曠殑Http瀹㈡埛绔痏SimpleHttpClient(URL)http://www.aygfsteel.com/tiandizhijian/articles/382655.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 15:03:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382655.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382655.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382655.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382655.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382655.htmlpackage 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();
        }
    }
   
}



]]>
03 涓涓畝鍗曠殑HttpServer_Command_CommandImplhttp://www.aygfsteel.com/tiandizhijian/articles/382651.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 14:14:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382651.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382651.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382651.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382651.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382651.htmlpackage com.tianhe.frm.http;

public interface Command
{
    public byte[] execute(String resource, char[] postData, String charset);
}


package com.tianhe.frm.http;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.PropertiesUtil;
import com.tianhe.frm.utils.StringUtil;

public class CommandImpl implements Command
{
    private String DEFAULT_RESPONSE;
   
    private String resultConfig = "response.properties";
   
    private Properties resultMapping;
   
    public CommandImpl()
    {
        init(resultConfig);
    }
   
    private void init(String resultConfig2)
    {
        try
        {
            if (StringUtil.isNotEmpty(resultConfig))
            {
                this.resultMapping = PropertiesUtil.loadProperties(ProcessorImpl.class, resultConfig);
            }
           
            this.DEFAULT_RESPONSE = createDefaultResponse();
        }
        catch (Exception e)
        {
            System.err.println("ProcessorImpl.init failed, ignored!");
        }
    }
   
    protected String createDefaultResponse()
    {
        String defaultResponse = GlobalConfig.getString("ProcessorImpl.DEFAULT_RESPONSE",
                "request url[%REQUEST_URL%] is not configured in reponse config!");
        return defaultResponse;
    }
   
    public byte[] execute(String resource, char[] postData, String charset)
    {
        String resKey = resource;
        resKey = resKey.replace("=", "__");
        resKey = resKey.replace(":", "--");
        resKey = resKey.replace("\"", "");
       
        byte[] resVal = createResultData(resource, resKey, charset);
       
        return resVal;
    }
   
    protected byte[] createResultData(String resource, String resKey, String charset)
    {
        String resVal = resultMapping.getProperty(resKey);
        if (StringUtil.isEmpty(resVal))
        {
            resVal = DEFAULT_RESPONSE.replaceAll("%REQUEST_URL%", resource);
        }
       
        byte[] resData = null;
        try
        {
            // resVal = URLEncoder.encode(resVal, charset);
            resData = resVal.getBytes(charset);
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
       
        return resData;
    }
   
    public static void main(String[] args)
    {
        CommandImpl s = new CommandImpl();
        Properties p = s.resultMapping;
        for (Object key : p.keySet())
        {
            System.out.println(key + "<->" + p.getProperty(key.toString()));
        }
    }
}



]]>
02 涓涓畝鍗曠殑HttpServer_Processor_ProcessorImplhttp://www.aygfsteel.com/tiandizhijian/articles/382652.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 14:14:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382652.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382652.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382652.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382652.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382652.htmlpackage com.tianhe.frm.http;

public interface Processor
{
    public byte[] process(String resource, char[] postData, String charset);
}

package com.tianhe.frm.http;

import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.ObjectUtil;

public class ProcessorImpl implements Processor
{
   
    public byte[] process(String resource, char[] postData, String charset)
    {
        Command command = createCommand(resource);
       
        byte[] resVal = command.execute(resource, postData, charset);
       
        return resVal;
    }
   
    private Command createCommand(String resource)
    {
        String type = getCommandType(resource);
       
        String clazzName = GlobalConfig.getString("ProcessorImpl.command." + type + ".CLASS_NAME",
                "com.tianhe.frm.http.CommandImpl");
        Command command = (Command)ObjectUtil.createObject(clazzName);

        return command;
    }
   
    private String getCommandType(String resource)
    {
        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 vsTemp;
    }
   
    public static void main(String[] args)
    {
        String type = null;
       
        {
            type = new ProcessorImpl().getCommandType("example/aaa.cmd1");
            System.out.println(type);
        }
       
        {
            type = new ProcessorImpl().getCommandType("example/aaa.cmd1?aaa.cmd=111");
            System.out.println(type);
        }
    }
   
}



]]>
02 涓涓畝鍗曠殑HttpServer_Processor_ProcessorImplhttp://www.aygfsteel.com/tiandizhijian/articles/382650.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 14:13:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382650.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382650.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382650.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382650.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382650.htmlpackage com.tianhe.frm.http;

public interface Processor
{
    public byte[] process(String resource, char[] postData, String charset);
}

package com.tianhe.frm.http;

import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.ObjectUtil;

public class ProcessorImpl implements Processor
{
   
    public byte[] process(String resource, char[] postData, String charset)
    {
        Command command = createCommand(resource);
       
        byte[] resVal = command.execute(resource, postData, charset);
       
        return resVal;
    }
   
    private Command createCommand(String resource)
    {
        String type = getCommandType(resource);
       
        String clazzName = GlobalConfig.getString("ProcessorImpl.command." + type + ".CLASS_NAME",
                "com.tianhe.frm.http.CommandImpl");
        Command command = (Command)ObjectUtil.createObject(clazzName);

        return command;
    }
   
    private String getCommandType(String resource)
    {
        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 vsTemp;
    }
   
    public static void main(String[] args)
    {
        String type = null;
       
        {
            type = new ProcessorImpl().getCommandType("example/aaa.cmd1");
            System.out.println(type);
        }
       
        {
            type = new ProcessorImpl().getCommandType("example/aaa.cmd1?aaa.cmd=111");
            System.out.println(type);
        }
    }
   
}



]]>
01 涓涓畝鍗曠殑HttpServer_SimpleHttpServer_Constanthttp://www.aygfsteel.com/tiandizhijian/articles/382649.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 14:11:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382649.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382649.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382649.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382649.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382649.html 

package com.tianhe.frm.http;

public interface Constant
{

   public static String CONTENT_LENGTH = "Content-Length:";   
}


package com.tianhe.frm.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.exception.OurRuntimeException;
import com.tianhe.frm.utils.LogUtil;
import com.tianhe.frm.utils.ObjectUtil;
import com.tianhe.frm.utils.StringUtil;

public class SimpleHttpServer implements Runnable
{
    private boolean debug = false;
   
    private String charset = "gbk";
   
    private ServerSocket serverSocket;// 鏈嶅姟鍣⊿ocket
   
    private static Processor processor = null;
   
    public SimpleHttpServer(int port, String charset, boolean debug)
    {
        this.debug = debug;
       
        if (StringUtil.isNotEmpty(charset))
        {
            this.charset = charset;
        }
       
        this.serverSocket = createServerSocket(port);
        if (this.serverSocket != null)
        {
            new Thread(this).start();
            System.out.println("SimpleHttpServer is running, port=" + port + ",charset=" + charset + ",debug=" + debug);
        }
    }
   
    private ServerSocket createServerSocket(int port)
    {
        ServerSocket ss = null;
       
        try
        {
            ss = new ServerSocket(port);
        }
        catch (IOException e)
        {
            throw new OurRuntimeException("createServerSocket failed", e);
        }
        return ss;
    }
   
    public void run()
    {
        while (true)
        {
            try
            {
                Socket socket = serverSocket.accept();
                new Thread(new ServerHandler(socket)).start();
            }
            catch (IOException e)
            {
                getLog().error("SimpleHttpServer run error", e);
            }
           
        }
    }
   
    class ServerHandler implements Runnable
    {
        private Socket socket;
       
        ServerHandler(Socket socket)
        {
            this.socket = socket;
        }
       
        public void run()
        {
            try
            {
                boolean firstFlag = false;
                String resource = "";
                String method = "";
                int len = 0;
               
                StringBuffer sb = new StringBuffer();
                String line;
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while ((line = in.readLine()) != null)
                {
                    sb.append(line).append("\n");
                   
                    if (!firstFlag)
                    {
                        resource = line.substring(line.indexOf('/') + 1, line.lastIndexOf('/') - 5);
                        resource = URLDecoder.decode(resource, charset);// 鍙嶇紪鐮?URL
                       
                        method = new StringTokenizer(line).nextElement().toString();// 鑾峰彇璇鋒眰鏂規硶, GET 鎴栬?POST
                        firstFlag = true;
                    }
                   
                    if (line.startsWith(Constant.CONTENT_LENGTH))
                    {
                        len = Integer.valueOf(line.substring(Constant.CONTENT_LENGTH.length()));
                    }
                   
                    if (line.equals(""))
                        break;
                }
               
                // 鏄劇ず POST 琛ㄥ崟鎻愪氦鐨勫唴瀹? 榪欎釜鍐呭浣嶄簬璇鋒眰鐨勪富浣撻儴鍒?br />                char[] reqData = null;
                if ("POST".equalsIgnoreCase(method) && len > 0)
                {
                    reqData = new char[len];
                    in.read(reqData);
                    sb.append(new String(reqData)).append("\n");
                }
               
                String requestString = sb.toString();
                System.out.println("-------------------------begin----------------------------");
                System.out.println(requestString);
                System.out.println("-------------------------end------------------------------");
               
                byte[] resData = processRequest(resource, reqData, charset);
                writeResponse(resource, resData, socket);
            }
            catch (IOException e)
            {
                throw new OurRuntimeException("ServerHandler run error", e);
            }
            finally
            {
                closeSocket(socket);
            }
        }
    }
   
    public byte[] processRequest(String resource, char[] data, String charset)
    {
        if (processor == null)
        {
            String clazzName = GlobalConfig.getString("ProcessorImpl.CLASS_NAME", "com.tianhe.frm.http.ProcessorImpl");
            processor = (Processor)ObjectUtil.createObject(clazzName);
        }
       
        byte[] resData = processor.process(resource, data, charset);
        return resData;
    }
   
    private void writeResponse(String resource, byte[] resData, Socket socket)
    {
        try
        {
            PrintStream out = new PrintStream(socket.getOutputStream(), true);
            out.println("HTTP/1.0 200 OK");
            out.println("Content-Type:text/html;charset=" + charset);
            if (resData != null)
            {
                out.println("Content-Length:" + resData.length);
                out.println();
                out.write(resData);
            }
            else
            {
                out.println("Content-Length: 0");
                out.println();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
   
    void closeSocket(Socket socket)
    {
        try
        {
            if (socket != null)
            {
                socket.close();
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
   
    private Logger getLog()
    {
        return LogUtil.getLog(getClass());
    }
   
    private static void usage()
    {
        System.out.println("Usage: java SimpleHttpServer -port 8080 -charset gbk -debug");
    }
   
    public static void main(String[] args)
    {
        try
        {
            String charset = "GBK";
            int port = 8080;
            boolean debug = false;
           
            if (args != null && args.length > 0)
            {
                for (int i = 0; i < args.length; i++)
                {
                   
                    if (args[i].startsWith("-port"))
                    {
                        port = Integer.valueOf(args[i + 1]);
                        i++;
                    }
                   
                    if (args[i].startsWith("-charset"))
                    {
                        charset = args[i + 1];
                        i++;
                    }
                   
                    if (args[i].startsWith("-debug"))
                    {
                        debug = true;
                        // i++;
                    }
                   
                    if (args[i].equalsIgnoreCase("-help"))
                    {
                        usage();
                        System.exit(0);
                    }
                }
            }
           
            new SimpleHttpServer(port, charset, debug);
        }
        catch (OurRuntimeException ex)
        {
            ex.printStackTrace();
        }
       
    }
}



]]>
00 涓涓畝鍗曠殑HttpServerhttp://www.aygfsteel.com/tiandizhijian/articles/382648.html縐嬪ぉ鐨勫北璋?/dc:creator>縐嬪ぉ鐨勫北璋?/author>Mon, 09 Jul 2012 14:10:00 GMThttp://www.aygfsteel.com/tiandizhijian/articles/382648.htmlhttp://www.aygfsteel.com/tiandizhijian/comments/382648.htmlhttp://www.aygfsteel.com/tiandizhijian/articles/382648.html#Feedback0http://www.aygfsteel.com/tiandizhijian/comments/commentRss/382648.htmlhttp://www.aygfsteel.com/tiandizhijian/services/trackbacks/382648.html
涓棿鍊熼壌浜嗕竴浜涚綉鍙嬬殑浠g爜錛屽湪姝よ嚧璋紝鍙傝濡備笅錛?br />
/**
 * 涓涓畝鍗曠殑鐢?Java Socket 緙栧啓鐨?HTTP 鏈嶅姟鍣ㄥ簲鐢? 婕旂ず浜嗚姹傚拰搴旂瓟鐨勫崗璁氫俊鍐呭浠ュ強 緇欏鎴風榪斿洖 HTML 鏂囨湰鍜屼簩榪涘埗鏁版嵁鏂囦歡(涓涓浘鐗?, 鍚屾椂灞曠ず浜?404, 200 絳夌姸鎬佺爜.
 * 棣栧厛榪愯榪欎釜紼嬪簭,鐒跺悗鎵撳紑Web嫻忚鍣?閿叆http://localhost,鍒欒繖涓▼搴忚兘澶熸樉紺哄嚭嫻忚鍣ㄥ彂閫佷簡閭d簺淇℃伅 騫朵笖鍚戞祻瑙堝櫒榪斿洖涓涓綉欏靛拰涓鍓浘鐗? 騫舵祴璇曞悓嫻忚鍣ㄥ璇? 褰撴祻瑙堝櫒鐪嬪埌 HTML 涓甫鏈夊浘鐗囧湴鍧鏃?
 * 鍒欎細鍙戝嚭絎簩嬈¤繛鎺ユ潵璇鋒眰鍥劇墖絳夎祫婧? 榪欎釜渚嬪瓙鍙互甯偍鐞嗚В Java 鐨?HTTP 鏈嶅姟鍣ㄨ蔣浠舵槸鍩轟簬 J2SE 鐨?Socket 絳夎蔣浠剁紪鍐欑殑姒傚康, 騫剁啛鎮?HTTP 鍗忚. 鐩稿弽鐨勭敤 Telnet
 * 榪炴帴鍒板凡鏈夌殑鏈嶅姟鍣ㄥ垯鍙互甯繖鐞嗚В嫻忚鍣ㄧ殑榪愯榪囩▼鍜屾湇鍔″櫒绔殑榪斿洖鍐呭.
 *
 * <pre>
 *       褰撶敤鎴峰湪Web嫻忚鍣ㄥ湴鍧鏍忎腑杈撳叆涓涓甫鏈塰ttp://鍓嶇紑鐨刄RL騫舵寜涓婨nter鍚?鎴栬呭湪Web欏甸潰涓煇涓互http://寮澶寸殑瓚呴摼鎺ヤ笂鍗曞嚮榧犳爣,HTTP浜嬪姟澶勭悊鐨勭涓涓樁孌?-寤虹珛榪炴帴闃舵灝卞紑濮嬩簡.HTTP鐨勯粯璁ょ鍙f槸80.
 *    闅忕潃榪炴帴鐨勫緩绔?HTTP灝辮繘鍏ヤ簡瀹㈡埛鍚戞湇鍔″櫒鍙戦佽姹傜殑闃舵.瀹㈡埛鍚戞湇鍔″櫒鍙戦佺殑璇鋒眰鏄竴涓湁鐗瑰畾鏍煎紡鐨凙SCII娑堟伅,鍏惰娉曡鍒欎負:
 * < Method > < URL > < HTTP Version > <\n>
 * { <Header>:<Value> <\n>}*
 * <\n>
 * { Entity Body }
 *    璇鋒眰娑堟伅鐨勯《绔槸璇鋒眰琛?鐢ㄤ簬鎸囧畾鏂規硶,URL鍜孒TTP鍗忚鐨勭増鏈?璇鋒眰琛岀殑鏈鍚庢槸鍥炶濺鎹㈣.鏂規硶鏈塆ET,POST,HEAD,PUT,DELETE絳?
 * 鍦ㄨ姹傝涔嬪悗鏄嫢騫蹭釜鎶ュご(Header)琛?姣忎釜鎶ュご琛岄兘鏄敱涓涓姤澶村拰涓涓彇鍊兼瀯鎴愮殑浜屽厓瀵?鎶ュご鍜屽彇鍊間箣闂翠互":"鍒嗛殧;鎶ュご琛岀殑鏈鍚庢槸鍥炶濺鎹㈣.甯歌鐨勬姤澶存湁Accept(鎸囧畾MIME濯掍綋綾誨瀷),Accept_Charset(鍝嶅簲娑堟伅鐨勭紪鐮佹柟寮?,Accept_Encoding(鍝嶅簲娑堟伅鐨勫瓧絎﹂泦),User_Agent(鐢ㄦ埛鐨勬祻瑙堝櫒淇℃伅)絳?
 *    鍦ㄨ姹傛秷鎭殑鎶ュご琛屼箣鍚庢槸涓涓洖杞︽崲琛?琛ㄦ槑璇鋒眰娑堟伅鐨勬姤澶撮儴鍒嗙粨鏉?鍦ㄨ繖涓猏n涔嬪悗鏄姹傛秷鎭殑娑堟伅瀹炰綋(Entity Body).鍏蜂綋鐨勪緥瀛愬弬鐪媓ttpRequest.txt.
 *     Web鏈嶅姟鍣ㄥ湪鏀跺埌瀹㈡埛璇鋒眰騫朵綔鍑哄鐞嗕箣鍚?瑕佸悜瀹㈡埛鍙戦佸簲絳旀秷鎭?涓庤姹傛秷鎭竴鏍?搴旂瓟娑堟伅鐨勮娉曡鍒欎負:
 * < HTTP Version> <Status Code> [<Message>]<\n>
 * { <Header>:<Value> <\n> } *
 * <\n>
 * { Entity Body }
 *    搴旂瓟娑堟伅鐨勭涓琛屼負鐘舵佽,鍏朵腑鍖呮嫭浜咹TTP鐗堟湰鍙?鐘舵佺爜鍜屽鐘舵佺爜榪涜綆鐭В閲婄殑娑堟伅;鐘舵佽鐨勬渶鍚庢槸鍥炶濺鎹㈣.鐘舵佺爜鐢?浣嶆暟瀛楃粍鎴?鏈?綾?
 * 鍙傜湅:HTTP搴旂瓟鐮佸強鍏舵剰涔?
 *
 * 1XX 淇濈暀
 * 2XX 琛ㄧず鎴愬姛
 * 3XX 琛ㄧずURL宸茬粡琚Щ璧?
 * 4XX 琛ㄧず瀹㈡埛閿欒
 * 5XX 琛ㄧず鏈嶅姟鍣ㄩ敊璇?
 * 渚嬪:415,琛ㄧず涓嶆敮鎸佹敼濯掍綋綾誨瀷;503,琛ㄧず鏈嶅姟鍣ㄤ笉鑳借闂?鏈甯歌鐨勬槸200,琛ㄧず鎴愬姛.甯歌鐨勬姤澶存湁:Last_Modified(鏈鍚庝慨鏀規椂闂?,Content_Type(娑堟伅鍐呭鐨凪IME綾誨瀷),Content_Length(鍐呭闀垮害)絳?
 *    鍦ㄦ姤澶磋涔嬪悗涔熸槸涓涓洖杞︽崲琛?鐢ㄤ互琛ㄧず搴旂瓟娑堟伅鐨勬姤澶撮儴鍒嗙殑緇撴潫,浠ュ強搴旂瓟娑堟伅瀹炰綋鐨勫紑濮?
 *    涓嬮潰鏄竴涓簲絳旀秷鎭殑渚嬪瓙:
 * HTTP/1.0 200 OK
 * Date: Moday,07-Apr-97 21:13:02 GMT
 * Server:NCSA/1.1
 * MIME_Version:1.0
 * Content_Type:text/html
 * Last_Modified:Thu Dec 5 09:28:01 1996
 * Coentent_Length:3107
 *
 * <HTML><HEAD><TITLE></HTML>
 *
 * 鍦ㄧ敤Java璇█瀹炵幇HTTP鏈嶅姟鍣ㄦ椂,棣栧厛鍚姩涓涓猨ava.net.ServerSocket鍦ㄦ彁渚涙湇鍔$殑绔彛涓婄洃鍚繛鎺?鍚戝鎴瘋繑鍥炴枃鏈椂,鍙互鐢≒rintWriter,浣嗘槸濡傛灉榪斿洖浜岃繘鍒舵暟鎹?鍒欏繀欏諱嬌鐢∣utputStream.write(byte[])鏂規硶,榪斿洖鐨勫簲絳旀秷鎭瓧絎︿覆鍙互浣跨敤String.getBytes()鏂規硶杞崲涓哄瓧鑺傛暟緇勮繑鍥?鎴栬呬嬌鐢≒rintStream鐨刾rint()鏂規硶鍐欏叆鏂囨湰,鐢╳rite(byte[])鏂規硶鍐欏叆浜岃繘鍒舵暟鎹?
 *
 * </pre>
 *
 * @author 鍒橀暱鐐?br /> * @version 1.0 2007-07-24 Sunday
 */



]]>
主站蜘蛛池模板: 黔江区| 子长县| 宜良县| 米林县| 合水县| 张家界市| 漾濞| 敖汉旗| 陵川县| 休宁县| 三原县| 周至县| 灵武市| 敦煌市| 堆龙德庆县| 义乌市| 莱芜市| 曲沃县| 健康| 平武县| 万载县| 嘉义市| 苍溪县| 永丰县| 积石山| 民县| 阿勒泰市| 确山县| 西丰县| 大方县| 广灵县| 老河口市| 玉树县| 泗阳县| 临泉县| 治县。| 河池市| 新田县| 威海市| 崇仁县| 五大连池市|