今天抽点时间,用java写个webserver,主要采用socket和多线程技术,实现一个java web服务器。
1、新建一个java项目webserver;
2、新建2个java文件:
WebServer.java和Processor.java
3、WebServer.java
package web.com;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer {
public void serverSocket(int prot){
try{
ServerSocket serversocket = new ServerSocket(prot);//监听80端口
while(true){
Socket socket = serversocket.accept();
new Processor(socket).start();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
int prot = 8800;
if(args.length==1){
prot = Integer.parseInt(args[0]);
}
new WebServer().serverSocket(prot);
}
}
4、Processor.java
package web.com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Processor extends Thread{
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String web_root = “F:\\Office\\webserver\\web”;//设置访问路径
public Processor(Socket socket){
this.socket = socket;
try {
in = socket.getInputStream();//输入流对象
out = new PrintStream(socket.getOutputStream());//输出流对象封装成PrintStream类型
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){ //完成输入输出流处理
String filename = parse(in);
try {
sendFile(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String parse(InputStream in){
BufferedReader br = new BufferedReader(new InputStreamReader(in));//把InputStream进行封装
String filename = null;
try {
String httpMessage = br.readLine();
String[] content = httpMessage.split(” “);
if(content.length != 3){ //判断返回的请求content是否等于3,如果不等于输出错误
sendErrorMessage(400 , “请求错误!”);
return null;
}
System.out.println(“code:”+content[0]+”,filename:”+content[1]+”,http version:”+content[2]);
filename = content[1];
} catch (IOException e) {
e.printStackTrace();
}//用于读取发送内容的第一行
return filename;
}
public void sendErrorMessage(int ErrorCode , String errorMessage){//该方法用于解析错误
out.println(“http:/1.0″+ErrorCode+” “+errorMessage);
out.println(“content-type:text/html”);
out.println();
out.println(“<html>”);
out.println(“<title>Error Message”);
out.println(“</title>”);
out.println(“<body>”);
out.println(“<h1>ErrorCode:”+ErrorCode+”,Message:”+errorMessage+”</h1>”);
out.println(“</body>”);
out.println(“</html>”);
out.flush();
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile(String filename) throws FileNotFoundException{ //该方法用于解析没有错误时的处理
File file = new File(Processor.web_root+filename);
if(file.exists()){
sendErrorMessage(404,”文件没有找到!”);
return;
}
try {
InputStream in = new FileInputStream(file);
byte content[] = new byte[(int)file.length()];//因为content是int类型,需要把file长度强制转换成int
in.read(content);
out.println(“http/1.0 200 queryfile”);
out.println(“content-length:”+content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
}catch(FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5、访问错误地址:http://localhost:88/haha.htm时,会把错误信息打印在页面中,如下图:
本文固定链接: http://www.test-life.org/?p=257 | 测试之路-My Test Space