HttpServer.java
package cn.hackcoder.action;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.ServerSocket;
import java.net.Socket;
import cn.hackcoder.model.Request;
import cn.hackcoder.model.Response;
public class HttpServer {
private static final String WEB_ROOT = System.getProperty("user.dir")
+ File.separator + "webapp";
private static final String SHUT_DWON = "/shutdown";
private ServerSocket server;
private Socket client = null;
private Request request = new Request();
private Response response = new Response();
private InputStream inputStream;
private OutputStream outputStream;
HttpServer(int port, int backlog, String serverName) throws IOException {
server = new ServerSocket(port, backlog,
Inet4Address.getByName(serverName));
}
public void execute() throws IOException {
while (true) {
Socket client = server.accept();
inputStream = client.getInputStream();
outputStream = client.getOutputStream();
request.setInputStream(inputStream);
String fileName = request.parseUri();
response.setOutputStream(client.getOutputStream());
response.sendResponse(WEB_ROOT, fileName);
if(inputStream!=null){
inputStream.close();
}
if(outputStream!=null){
inputStream.close();
}
}
}
public static void main(String[] args) {
try {
HttpServer httpSever = new HttpServer(8888, 10, "127.1.1.111");
httpSever.execute();
httpSever.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
try {
if (server != null && !server.isClosed())
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Request
package cn.hackcoder.model;
import java.io.IOException;
import java.io.InputStream;
public class Request {
private InputStream inputStream = null;
private byte[] bytes = new byte[2048];
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* 返回解析的uri
*
* @return
* @throws IOException
*/
public String parseUri() throws IOException {
StringBuffer sb = new StringBuffer();
int ch = inputStream.read(bytes);
for (int i = 0; i < ch; i++) {
sb.append((char) bytes[i]);
}
int index1 = sb.indexOf(" ");// 获取请求资源
int index2 = sb.substring(index1 + 1).indexOf(" ");
if (index1 < index2) {
return sb.substring(index1 + 1, index1 + index2 + 1);
}
return null;
}
}
Response
package cn.hackcoder.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
public class Response {
private OutputStream outputStream;
private byte[] bytes = new byte[2048];
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
/**
* 响应消息
*
* @throws IOException
*/
public void sendResponse(String location, String fileName)
throws IOException {
if(location==null||fileName==null){
return;
}
File file = new File(location, fileName);
if (!file.exists()) {
PrintStream printStream = new PrintStream(getOutputStream());
printStream.print("<h1>ERROR</h1>");
printStream.close();
}
// 读取文件内容,用于返回响应
InputStream inputStream = new FileInputStream(file);
int len = inputStream.read(bytes);
while (len != -1) {
outputStream.write(bytes, 0, len);
len = inputStream.read(bytes);
}
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}