此源码将运行在服务器端,使用serverSocket类对端口和Ip地址进行监听(监听模式是使用死循环在进行监听),当接收到客户端对监听IP和端口进行访问时,会进入到HttpServer类中对客户端请求进行解析,使用request来对请求流中的url读取出来,使用response类将请求目标文件读取到响应流中,响应给客户端!将对/SHUTDOWN访问时退出监听循环。
客户端访问时例子:http://localhost:8080/index.html
服务器类
package com.util;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
//文件所方路径信息
public static final String WEB_ROOT=System.getProperty("user.dir")+File.separator+"webroot";
public static final String SHUTDOWN_COMMAND="/SHUTDOWN";
private boolean shutdown= false;
public void await()
{
//在服务端创建连接访问监听
ServerSocket serverSocket=null;
//监听端口8080
int port=8080;
try{ //实时监听
serverSocket = new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));
}catch(Exception e){
System.exit(1); e.printStackTrace();
}
//在为将监听关闭的情况下进程将进入死循环中
while(!shutdown){
Socket socket = null;
InputStream input = null;
OutputStream output=null;
try{
//当获得客户端8080端口访问后可以根据服务器套接类的accept方法获得与客户端通信的 套接类
socket = serverSocket.accept();
//获得客户端的请求数据
input = socket.getInputStream();
//获得套接类的输出流
output = socket.getOutputStream();
//读取客户端请求的请求数据读出
Request request= new Request(input);
//读取客户端请求数据的url访问地址
request.parse();
//对客户端响应数据进行封装
Response response = new Response(output);
//将解析好的请求数据赋值给响应数据
response.setRequest(request);
//将相应数据写入到输出流中
response.sendStaticResource();
//套接类关闭
socket.close();
//当访问地址为/SHUTDOWN将监听关闭
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
}catch(Exception e){
e.printStackTrace();
continue;
} } }
//启动监听
public static void main(String[]args){ HttpServer httpserver = new HttpServer(); httpserver.await(); }
}
客户端请求解析类
package com.util;
import java.io.InputStream;
public class Request {
//客户端请求的输入流
private InputStream input;
//读取客户端请求的url地址
private String uri;
public Request(InputStream input){ this.input=input; }
//解析客户端请求的输入流
public void parse(){
StringBuffer request = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try{
i =input.read(buffer);
}catch(Exception e){
e.printStackTrace(); i=-1;
}
for(int j=0;j<i;j++){
request.append((char)buffer[j]);
}
System.out.println(request.toString());
uri = parseUri(request.toString());
} //将请求输入流中的url读出
private String parseUri(String requestString){
int index1,index2;
index1 = requestString.indexOf(" ");
if(index1!=-1){
index2 = requestString.indexOf(" ",index1+1);
if(index2>index1){
return requestString.substring(index1+1,index2);
}
}
return null;
}
public String getUri(){ return uri; }
}
客户端响应封装类
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Response {
private static final int BUFFER_SIZE =1024;
//获得套接类的响应流信息
OutputStream output;
//解析好的请求流信息
Request request;
public Response(OutputStream output){
this.output = output;
}
public void setRequest(Request request){
this.request=request;
}
//根据请求流解析的URL信息找到相应的文件 读取文件内容到响应流中 响应给客户端
public void sendStaticResource() throws IOException{
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try{
//获得文件内容
File file = new File(HttpServer.WEB_ROOT,request.getUri());
//如果文件存在并读取到内容时 将文件内容写入到客户端响应流中
if(file.exists()){
fis = new FileInputStream(file);
int ch = fis.read(bytes,0,BUFFER_SIZE);
while(ch!=-1){
output.write(bytes,0,ch);
ch = fis.read(bytes,0,BUFFER_SIZE);
}
//如果不存在时将错误信息显示给客户端相应流中
}else{
String errorMessage="HTTP/1.1 404 File not Found\\r\\n Content-Type:text/html \\r\\n Content-Length:23\\r\\n <h1>File not Found</h1>"; output.write(errorMessage.getBytes());
}
}catch(Exception e){
e.printStackTrace();
//关闭连接
}finally{
if(fis!=null){ fis.close(); }
}
}
}

被折叠的 条评论
为什么被折叠?



