我的簡(jiǎn)單HTTP服務(wù)器
在BLOGJAVA上看到一篇關(guān)于HTTP SERVER的簡(jiǎn)單實(shí)現(xiàn),博文網(wǎng)址為:http://www.aygfsteel.com/beansoft/archive/2007/06/25/126049.html,關(guān)于HTTP 協(xié)議更多信息,請(qǐng)參考:WIKI,RFC2616。下面我的實(shí)現(xiàn):
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;
/**
* 簡(jiǎn)單HTTP服務(wù)器
*
* @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服務(wù)器已經(jīng)啟動(dòng)");
}
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("請(qǐng)求內(nèi)容如下:\n---------------------");
String line = reader.readLine();
System.out.println(line);
// 請(qǐng)求的資源
String resource = line.substring(line.indexOf("/"), line.lastIndexOf("/") - 5);
resource = URLDecoder.decode(resource, "UTF-8");
// 請(qǐng)求方法: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協(xié)議:空行表示信息結(jié)束
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>");
// 測(cè)試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>");
// 測(cè)試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();
}
}
}
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;
/**
* 簡(jiǎn)單HTTP服務(wù)器
*
* @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服務(wù)器已經(jīng)啟動(dòng)");
}
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("請(qǐng)求內(nèi)容如下:\n---------------------");
String line = reader.readLine();
System.out.println(line);
// 請(qǐng)求的資源
String resource = line.substring(line.indexOf("/"), line.lastIndexOf("/") - 5);
resource = URLDecoder.decode(resource, "UTF-8");
// 請(qǐng)求方法: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協(xié)議:空行表示信息結(jié)束
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>");
// 測(cè)試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>");
// 測(cè)試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();
}
}
}
運(yùn)行之后,控制臺(tái)的輸出類似如下:



















































頁(yè)面運(yùn)行效果類似如下:

另,推薦一個(gè)不錯(cuò)的命令行“netstat -an |find /i "listening" >good.txt”,查看當(dāng)前處于監(jiān)聽(tīng)狀態(tài)的IP及端口,并將得到的信息重定向到good.txt中,形如:







posted on 2009-05-23 17:51 俊星 閱讀(518) 評(píng)論(0) 編輯 收藏