俊星的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 俊星 阅读(881) | 评论 (0)编辑 收藏

我的简单HTTP服务器

在BLOGJAVA上看到一篇关于HTTP SERVER的简单实现,博文网址为:http://www.blogjava.net/beansoft/archive/2007/06/25/126049.html
关于HTTP 协议更多信息,请参考:WIKIRFC2616。下面我的实现:
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 俊星 阅读(510) | 评论 (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 俊星 阅读(4174) | 评论 (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 俊星 阅读(234) | 评论 (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 俊星 阅读(144) | 评论 (0)编辑 收藏

仅列出标题
共10页: First 上一页 2 3 4 5 6 7 8 9 10 下一页