由于HTTP是在TCP/IP协议之上实现的,因此可以使用Java中的Socket处理HTTP请求,以下为一个简单的实现。
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyCustomHttpServer {
public static void main(String[] args) {
try{
ServerSocket serverSocket = new ServerSocket(9090);
while (true){
Socket accept = serverSocket.accept();
System.out.println("收到新连接");
InputStream ins = accept.getInputStream();
String methoName = getMethodName(ins);
String url = getUri(ins);
System.out.println(methoName);
System.out.println(url);
PrintStream writer = new PrintStream(accept.getOutputStream());
writer.println("HTTP/1.0 200 OK");// 返回应答消息,并结束应答
switch (url){
case "text":
System.out.println("文本请求");
writer.println("Content-Type: text/plain;charset=utf-8");
String msg = "你好,小诸葛的博客";
writer.println("Content-Length: " + msg.length()*3);
writer.println();// 根据 HTTP 协议, 空行将结束头信息
writer.write(msg.getBytes("utf-8"));
break;
case "viewpicture":
System.out.println("预览图片请求");
InputStream inputStream1 = new FileInputStream("e://bgpic.jpg");
writer.println("Content-Type: image/jpeg");
writer.println("Content-Length: " + inputStream1.available());// 返回内容字节数
writer.println();// 根据 HTTP 协议, 空行将结束头信息
byte[] buf1 = new byte[inputStream1.available()];
inputStream1.read(buf1);
writer.write(buf1);
writer.close();
inputStream1.close();
break;
case "downloadpicture":
System.out.println("下载图片请求");
InputStream inputStream2 = new FileInputStream("e://bgpic.jpg");
writer.println("Content-Type: application/binary");
writer.println("Content-Length: " + inputStream2.available());// 返回内容字节数
writer.println();// 根据 HTTP 协议, 空行将结束头信息
byte[] buf2 = new byte[inputStream2.available()];
inputStream2.read(buf2);
writer.write(buf2);
writer.close();
inputStream2.close();
break;
case "html":
System.out.println("网页请求");
writer.println("Content-Type: text/html;charset=utf-8");
String html = "<html><h1 style='color:#3982ff;text-align:center;'>hello,小诸葛的博客!</h1></html>";
writer.println("Content-Length: " + html.length());
writer.println();// 根据 HTTP 协议, 空行将结束头信息
writer.write(html.getBytes("utf-8"));
default:
System.out.println("无法处理该请求");
break;
}
accept.close();
System.out.println("请求结束。。。。。");
}
}catch (Exception e){
e.printStackTrace();
}
}
//从输入流解析出方法名
public static String getMethodName(InputStream inputStream){
try{
int b = inputStream.read();
byte[] methodBytes = new byte[10];
int i = 0;
while ((b != -1) && ((char)b != ' ') ){
methodBytes[i] = (byte) b;
i++;
b = inputStream.read();
}
return new String(methodBytes);
}catch (Exception e){
e.printStackTrace();
}
return "无";
}
//从输入流解析出uri
public static String getUri(InputStream inputStream){
try{
inputStream.read();
int b = inputStream.read();
byte[] uriBytes = new byte[24];
int i = 0;
while ((b != -1) && ((char)b != ' ') ){
uriBytes[i] = (byte) b;
i++;
b = inputStream.read();
}
byte[] realUri = new byte[i];
System.arraycopy(uriBytes,0, realUri,0, i);
return new String(realUri);
}catch (Exception e){
e.printStackTrace();
}
return "无";
}
}
这里只做了四种类型的请求,在浏览器中输入http://localhost:9090/资源类型(text、viewpicture、downloadpicture、html) 访问,就会得到一个对应的响应。