<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    俊星的BLOG

    #

    我的DWR之簡單實現

         摘要: 1、測試類: package mydwr.test; import java.util.Date; public class Single {     public String printMsg(String msg, boolean boo...  閱讀全文

    posted @ 2009-05-24 18:04 俊星 閱讀(882) | 評論 (0)編輯 收藏

    我的簡單HTTP服務器

    在BLOGJAVA上看到一篇關于HTTP SERVER的簡單實現,博文網址為:http://www.tkk7.com/beansoft/archive/2007/06/25/126049.html,
    關于HTTP 協議更多信息,請參考:WIKI,RFC2616。下面我的實現:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URLDecoder;

    /**
     * 簡單HTTP服務器
     * 
     * 
    @author kinkding
     
    */
    public class MyHTTPServer implements Runnable {
        ServerSocket server;
        
    int port = 80;

        
    public MyHTTPServer() throws IOException {
            server 
    = new ServerSocket(port);
            
    new Thread(this).start();
            System.out.println(
    "HTTP服務器已經啟動");
        }

        
    public void run() {
            
    while (true) {
                
    try {
                    Socket client 
    = server.accept();
                    System.out.println(
    "接收到客戶端:" + client);
                    BufferedReader reader 
    = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println(
    "請求內容如下:\n---------------------");
                    String line 
    = reader.readLine();
                    System.out.println(line);
                    
    // 請求的資源
                    String resource = line.substring(line.indexOf("/"), line.lastIndexOf("/"- 5);
                    resource 
    = URLDecoder.decode(resource, "UTF-8");
                    
    // 請求方法:GET/POST
                    String method = line.split(" ")[0];
                    
    int length = 0;
                    
    while ((line = reader.readLine()) != null && line.length() > 0) {
                        
    if (line.startsWith("Content-Length"&& length == 0) {
                            length 
    = Integer.parseInt(line.substring(line.indexOf(":"+ 1).trim());
                        }
                        System.out.println(line);
                    }
                    System.out.println(
    "---------------------");
                    String postData 
    = "";
                    
    if (method.equals("POST"&& length > 0) {
                        
    char cbuf[] = new char[length];
                        reader.read(cbuf, 
    0, length);
                        postData 
    = new String(cbuf);
                    }
                    
    if (resource.endsWith(".jpg")) {
                        
    this.sendFile("happyTime.jpg", client);
                        client.close();
                    } 
    else if (resource.endsWith(".do")) {
                        
    // 返回404
                        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
                        out.println(
    "HTTP/1.0 404 Not found");
                        out.println();
                        out.close();
                    } 
    else {
                        PrintWriter out 
    = new PrintWriter(client.getOutputStream(), true);
                        out.println(
    "HTTP/1.0 200 OK");
                        out.println(
    "Content-Type:text/html;charset=GBK");
                        out.println();
    // HTTP協議:空行表示信息結束

                        out.println(
    "<UL>");
                        out.println(
    "<LI>Hello World");
                        out.println(
    "<LI><img src='anyJPG.jpg'/>");
                        out.println(
    "<LI>resource:" + resource);
                        out.println(
    "<LI>method:" + method);
                        out.println(
    "<LI><a href='foobar.do'>foobar.do</a>");
                        out.println(
    "</UL>");
                        
    // 測試POST
                        out.println("<form method='post' action='/'>");
                        out.println(
    "<input type='text' name='name' value=''/>");
                        out.println(
    "<input type='submit' value='POST'/>");
                        out.println(
    "POST DATA:" + postData);
                        out.println(
    "</form>");
                        
    // 測試GET
                        out.println("<form method='get' action='/'>");
                        out.println(
    "<input type='text' name='job' value=''/>");
                        out.println(
    "<input type='submit' value='GET'/>");
                        out.println(
    "</form>");

                        out.close();
                        client.close();
                    }

                } 
    catch (Exception e) {
                    
    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }

        
    private void sendFile(String name, Socket client) throws IOException {
            PrintStream out 
    = new PrintStream(client.getOutputStream(), true);
            File fileToSend 
    = new File(name);
            
    if (fileToSend.exists() && !fileToSend.isDirectory()) {
                out.println(
    "HTTP/1.0 200 OK");
                out.println(
    "Content-Type:application/binary");
                out.println(
    "Content-Length:" + fileToSend.length());
                out.println();

                FileInputStream fis 
    = new FileInputStream(fileToSend);
                
    byte buf[] = new byte[1024];
                
    int size = 0;
                
    while ((size = fis.read(buf)) > 0) {
                    out.write(buf, 
    0, size);
                }
                out.close();
                fis.close();
            }
        }

        
    public static void main(String[] args) {
            
    try {
                
    new MyHTTPServer();
            } 
    catch (IOException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    運行之后,控制臺的輸出類似如下:
    HTTP服務器已經啟動
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1198,localport=80]
    請求內容如下:
    ---------------------
    GET / HTTP/
    1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1199,localport=80]
    請求內容如下:
    ---------------------
    GET /anyJPG.jpg HTTP/
    1.1
    Accept: */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1218,localport=80]
    請求內容如下:
    ---------------------
    POST / HTTP/
    1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Content-Length: 
    7
    Connection: Keep-Alive
    Cache-Control: no-cache
    ---------------------
    接收到客戶端:Socket
    [addr=/127.0.0.1,port=1231,localport=80]
    請求內容如下:
    ---------------------
    GET /?job
    =aaa HTTP/1.1
    Accept: image/gif
    , image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Referer: http://localhost/
    Accept-Language: zh-cn
    Accept-Encoding: gzip
    , deflate
    User-Agent: Mozilla/
    4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: localhost
    Connection: Keep-Alive
    ---------------------

    頁面運行效果類似如下:
     
    另,推薦一個不錯的命令行“netstat -an |find /i "listening" >good.txt”,查看當前處于監聽狀態的IP及端口,并將得到的信息重定向到good.txt中,形如:
      TCP    0.0.0.0:80             0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:135            0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:445            0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:2366           0.0.0.0:0              LISTENING
      TCP    
    0.0.0.0:19771          0.0.0.0:0              LISTENING
      TCP    
    127.0.0.1:1031         0.0.0.0:0              LISTENING

    posted @ 2009-05-23 17:51 俊星 閱讀(515) | 評論 (0)編輯 收藏

    JAVA圖像縮放處理

         摘要: 今天在網上看到了一篇關于JAVA圖像處理的文章,博主貼出了一個處理類:特點是高品質縮小,具體代碼如下: import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; im...  閱讀全文

    posted @ 2009-05-23 13:37 俊星 閱讀(4181) | 評論 (1)編輯 收藏

    我的DWR之DefaultContainer

         摘要: DWR中采用DefaultContainer來加載默認的配置信息,下面是我的實現: import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; im...  閱讀全文

    posted @ 2009-05-20 22:45 俊星 閱讀(236) | 評論 (0)編輯 收藏

    我的DWR之ThreadLocal學習

    ThreadLocal實際是一個thread local variable(線程局部變量),其目的是為每一個使用該變量的線程都提供一個變量值的副本,更多內容可以參考該文章(http://www.cnblogs.com/zjblue/articles/495123.html),下面是我試用:
    public class ThreadTest {
        
    public static void main(String[] args) {
            System.out.println(
    "main " + ThreadNum.getNum());
            
    new ThreadOne().start();
            
    new ThreadTwo().start();
        }

    }


    class ThreadOne extends Thread {
        
    public void run() {
            System.out.println(
    "Thread1 " + ThreadNum.getNum());
        }

    }


    class ThreadTwo extends Thread {
        
    public void run() {
            System.out.println(
    "Thread2 " + ThreadNum.getNum());
        }

    }


    class ThreadNum {
        
    private static int num = 0;
        
    private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>() {
            
    protected synchronized Integer initialValue() {
                
    return new Integer(num++);
            }

        }
    ;

        
    public static int getNum() {
            
    return tl.get().intValue();
        }

    }

    輸出如下:
    main 0
    Thread1 1
    Thread2 2

    posted @ 2009-05-20 21:28 俊星 閱讀(148) | 評論 (0)編輯 收藏

    僅列出標題
    共10頁: First 上一頁 2 3 4 5 6 7 8 9 10 下一頁 
    主站蜘蛛池模板: 久久精品夜色国产亚洲av| 免费中文字幕在线观看| 久久综合图区亚洲综合图区| 一级毛片在线完整免费观看| 亚洲?V乱码久久精品蜜桃| 国内成人精品亚洲日本语音| 无码欧精品亚洲日韩一区夜夜嗨| 国产精品亚洲va在线观看| 国产免费av片在线播放 | 在线视频亚洲一区| 国产精品免费播放| 无套内谢孕妇毛片免费看看| mm1313亚洲精品国产| 国产日韩精品无码区免费专区国产| 国产成人亚洲精品狼色在线| 国产免费阿v精品视频网址| 亚洲高清视频在线观看| 在免费jizzjizz在线播| 亚洲色大情网站www| 亚洲VA综合VA国产产VA中| 国产在线精品一区免费香蕉| 久久亚洲AV无码精品色午夜麻| 免费国产黄网站在线观看| 亚洲性线免费观看视频成熟| 四虎永久精品免费观看| 成人无码视频97免费| 亚洲精品**中文毛片| 日本视频免费在线| 中文字幕免费在线看线人动作大片| 亚洲一区二区电影| 好吊妞788免费视频播放| 人人狠狠综合久久亚洲高清| g0g0人体全免费高清大胆视频| 亚洲a在线视频视频| 午夜成年女人毛片免费观看| www免费黄色网| 亚洲综合一区国产精品| 中文字幕无码精品亚洲资源网| 美女无遮挡免费视频网站 | 亚洲精品欧洲精品| 国产免费小视频在线观看|