您的位置:首页 > 编程语言 > Java开发

用java 实现一个简单的web 服务器

2011-12-24 09:15 1061 查看
import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

/**

* Socket + Thread + FileIO

*

* 服务器运行机制:

* 1. 启动服务器,处于运行状态

* 2. 能接收到请求

* 3. 分析请求内容

* 4. 处理响应内容

* 5. 发送响应内容到客户端

*

* @author Administrator

*

*/

public class Server

{

private int port = 8888;

/**

* 启动服务器

*/

public void start()

{

try

{

// 实例化一个服务器端Socket对象,用于接收客户端的连接信息

ServerSocket server = new ServerSocket(port);

while(true)

{

// 接收到的socket对象中 包含了http请求信息

Socket socket = server.accept();

System.out.println("接收到一个请求!");

// 将socket对象交给线程类进行 单独的处理,从而可以同时响应多个用户的请求

Processor p = new Processor(socket);

new Thread(p).start();

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

/**

* 简单的服务器程序,通过main方法来启动服务器

* @param args

*/

public static void main(String[] args)

{

new Server().start();

}

}

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.OutputStream;

import java.io.OutputStreamWriter;

import java.net.Socket;

/**

* Processor 用于接收请求内容,分析请求内容,处理响应,发送响应

* @author Administrator

*

*/

public class Processor implements Runnable

{

private Socket socket;

// 定义虚拟服务器所在的根目录

// 当访问请求地址为 http://localhost:8888/doc/index.html
// 程序将读取并返回 WEB_ROOT\doc\index.html 文件的内容

private final static String WEB_ROOT = "E:\\java学习\\web 作业\\浮动广告";

public Processor(Socket s)

{

this.socket = s;

}

@Override

public void run()

{

// 分析请求内容

String filePath = parse();

// 处理响应,读取页面文件内容

String fileContent = readFile(filePath);

if(fileContent != null)

{

// 发送响应

sendResult(200, "OK", fileContent);

}

}

/**

* 解析请求内容,根据请求中的URL内容,获取请求对应的页面文件路径

* @return 页面相对路径

*/

public String parse()

{

try

{

InputStream in = socket.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String requestContent = reader.readLine();

// System.out.println(requestContent);

String[] contents = requestContent.split(" ");

String url = contents[1];

// System.out.println(url);

// in.close();

// reader.close();

return url;

}

catch (IOException e)

{

e.printStackTrace();

}

return null;

}

/**

* 根据传入的文件相对路径,通过文件IO流读取内容,并返回

* 如果没有找到文件,发送404错误响应

* @param filePath

* @return

*/

public String readFile(String filePath)

{

String absFilePath = WEB_ROOT + filePath;

File file = new File(absFilePath);

System.out.println(file.getAbsolutePath() + " 文件是否存在:"+file.exists());

// File IO流读取文件内容

if(file.exists())

{

try

{

byte[] buff = new byte[(int) file.length()];

FileInputStream fis = new FileInputStream(file);

fis.read(buff);

String fileContent = new String(buff);

fis.close();

return fileContent;

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

else

{

sendResult(404, "File not found", "404... 你懂的!");

}

return null;

}

/**

* 通过响应发送响应内容

* @param errorCode

* @param errorMessage

*/

public void sendResult(int code, String message, String result)

{

// 通过socket对象的输出流 发送响应内容

try

{

OutputStream os = socket.getOutputStream();

OutputStreamWriter osw = new OutputStreamWriter(os);

osw.write("HTTP/1.1 " + code + " " + message + "\r\n");

osw.write("Content-Length:" + result.getBytes().length + "\r\n");

osw.write("Content-Type:text/html\r\n");

osw.write("\r\n");

osw.write(result);

osw.flush();

osw.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: