关于静态文件服务器,我觉得博文共赏:Node.js静态文件服务器实战写的不错,简单易懂,思路清晰,不过使用Nodejs写的。我本人更熟悉Java,所以就用Java写了一个简易版本。

此版本主要实现以下功能:

1 . 对于请求的资源,若处理成功,返回200

2 . 对于请求的资源,若服务器上不存在,返回404

3 . 多线程处理浏览器发出的请求

4 . MIME类型支持

此项目的完整代码,可以到java-staticfile中download.

先贴项目工程图

Handler.java

对请求头进行处理

public class Handler implements Runnable {
Functions functions = new Functions();
MIME mime = new MIME();
HashMap type = mime.getMime();
String contentType = null;
public String encoding = "UTF-8";
private Socket client;
PrintWriter out = null;
public Handler(Socket client) {
this.client = client;
}
public void run() {
if (client != null) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String header = reader.readLine();
System.out.println("客户端发送的请求信息: >>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println(header);// 读取所有浏览器发送过来的请求参数头部的所有信息
String resource = (String) header.split(" ")[1];// 获得请求的资源的地址
System.out.println("客户端发送的请求信息结束 <<<<<<<<<<<<<<<<<<<<<<<<<
System.out.println("用户请求的资源resource是:" + resource);
System.out.println();
String suffix = null;
if (resource.equals("/")) {
resource = "/index.html";
String[] names = resource.split("\\.");
suffix = names[names.length - 1];
contentType = type.get(suffix);
} else {
String[] names = resource.split("\\.");
suffix = names[names.length - 1];
contentType = type.get(suffix);
}
String path = "/home/sunyan/code/public/" + resource;
File file = new File(path);
if (file.exists()) {
if (suffix.equals("png") || suffix.equals("jpg") || suffix.equals("jpeg")) {
functions.readImg(file, client, contentType);
} else {
functions.readFile(file, client, contentType);
}
} else {
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.println("HTTP/1.0 404 NOTFOUND");// 返回应答消息,并结束应答
out.println("Content-Type:text/html;charset=UTF-8");
out.println();// 根据 HTTP 协议, 空行将结束头信息
out.println("对不起,您寻找的资源在本服务器上不存在");
out.close();
functions.closeSocket(client);
} // file.exists
} catch (Exception e) {
System.out.println("HTTP服务器错误:" + e.getLocalizedMessage());
}
}
}
}

Functions.java

包括三个函数

closeSocket():用于关闭socket

readImg() readFile():用于读取静态文件

若文件存在,则调用方法读取文件,正常状态下则发送读取到的文件给客户端,并返回200状态。

若服务器读取文件出错,则返回500状态。

若文件不存在,则返回404状态。

public class Functions {
public void closeSocket(Socket socket) {
try {
socket.close();
System.out.println(socket + "离开了HTTP服务器");
System.out.println();
System.out.println();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void readImg(File file, Socket client, String contentType) {
PrintStream out = null;
FileInputStream fis = null;
try {
out = new PrintStream(client.getOutputStream(), true);
fis = new FileInputStream(file);
byte data[] = new byte[fis.available()];
out.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答
out.println("Content-Type:" + contentType + ";charset=UTF-8");
out.println("Content-Length: " + file.length());
out.println();// 根据 HTTP 协议, 空行将结束头信息
fis.read(data);
out.write(data);
fis.close();
} catch (Exception e) {
out.println("HTTP/1.0 500");// 返回应答消息,并结束应答
out.println("");
out.flush();
} finally {
out.close();
closeSocket(client);
}
}
public void readFile(File file, Socket client, String contentType) {
PrintWriter out = null;
try {
out = new PrintWriter(client.getOutputStream(), true);
FileReader in = new FileReader(file);
BufferedReader breader = new BufferedReader(in);
String s = null;
StringBuffer sbu = new StringBuffer();
out.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答
out.println("Content-Type:" + contentType + ";charset=UTF-8");
out.println();// 根据 HTTP 协议, 空行将结束头信息
while ((s = breader.readLine()) != null) {
out.println(s);
}
} catch (Exception e) {
out.println("HTTP/1.0 500");// 返回应答消息,并结束应答
out.println("");
out.flush();
} finally {
out.close();
closeSocket(client);
}
}
}

MIME.java

MIME类型支持

因为服务器同时要存放html, css, js, png, gif, jpg等等文件。并非每一种文件的MIME类型都是text/html的。因此需要支持MIME。

支持MIME的话,就需要一张映射表。

public class MIME {
HashMap mime = new HashMap();
public MIME() {
mime.put("css", "text/css");
mime.put("gif", "image/gif");
mime.put("html", "text/html;charset=utf-8");
mime.put("ico", "image/x-icon");
mime.put("jpeg", "image/jpeg");
mime.put("jpg", "image/jpeg");
mime.put("js", "text/javascript");
mime.put("json", "application/json");
mime.put("pdf", "application/pdf");
mime.put("png", "image/png");
mime.put("svg", "image/svg+xml");
mime.put("swf", "application/x-shockwave-flash");
mime.put("tiff", "image/tiff");
mime.put("txt", "text/plain;charset=utf-8");
mime.put("wav", "audio/x-wav");
mime.put("wma", "audio/x-ms-wma");
mime.put("wmv", "video/x-ms-wmv");
mime.put("xml", "text/xml");
}
public HashMap getMime() {
return mime;
}
}
MultiThreadServer.java

监听客户端发出的请求,并创建线程对其进行处理

public class MultiThreadServer {
public static void main(String[] args) throws Exception {
int port = 20012;// 标准HTTP端口
ServerSocket server = new ServerSocket(20012);
Socket client = null;
System.out.println("静态文件服务器正在运行,端口:" + port);
System.out.println();
while (true) {
client = server.accept();
System.out.println(client + "连接到HTTP服务器");
Handler handler = new Handler(client);
new Thread(handler).start();
}
}
}

测试过程