java 实现http响应

来源:互联网 发布:淘宝集市店 编辑:程序博客网 时间:2024/06/01 12:54
  1. public class HttpServer {  
  2.   
  3.     //WEB_ROOT该服务器的根目录,这个目录可以自己定义,主要是服务器响应的文件所在目录  
  4.     public static final String WEB_ROOT = System.getProperty("user.dir")+File.separator+"webroot";  
  5.       
  6.     public static void main(String[] args) {  
  7.         HttpServer server = new HttpServer();  
  8.         System.out.println("WEB_ROOT:"+WEB_ROOT);  
  9.         server.await();  
  10.     }  
  11.     public void await(){  
  12.         ServerSocket serverSocket = null;  
  13.         int port = 8080;  
  14.         try {  
  15.             serverSocket = new ServerSocket(port,1,InetAddress.getByName("127.0.0.1"));  
  16.             System.out.println("等待来自浏览器的连接...");  
  17.             while(true){  
  18.         try {  
  19.             Socket socket = null;  
  20.             InputStream input = null;  
  21.             OutputStream output = null;  
  22.             socket = serverSocket.accept();  
  23.             input  = socket.getInputStream();  
  24.             output = socket.getOutputStream();  
  25.             //封装request请求  
  26.             Request request = new Request(input);  
  27.             request.parse();  
  28.             //封装response对象  
  29.             Response response = new Response(output);  
  30.             response.setRequest(request);  
  31.             response.sendStaticResource();  
  32.             socket.close();  
  33.             } catch (Exception e) {  
  34.                     e.printStackTrace();  
  35.                     continue;  
  36.                 }  
  37.             }  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }     
  41.     }  
  42. }  
可以看到,浏览器发送过来的请求信息是通过socket.getInputStream();得到,并将其封装到Request中,让Request去处理请求信息。

2、请求对象Request

[java] view plain copy
  1. public class Request {  
  2.       
  3.     //输入流  
  4.     private InputStream input;  
  5.       
  6.     private String uri;  
  7.     public Request(InputStream inputStream){  
  8.         this.input = inputStream;  
  9.     }  
  10.       
  11.     public InputStream getInput() {  
  12.         return input;  
  13.     }  
  14.     public void setInput(InputStream input) {  
  15.         this.input = input;  
  16.     }  
  17.     public String getUri() {  
  18.         return uri;  
  19.     }  
  20.     public void setUri(String uri) {  
  21.         this.uri = uri;  
  22.     }  
  23.       
  24.     //从套接字中读取字符信息  
  25.     public void parse(){  
  26.           
  27.             StringBuffer request = new StringBuffer(2048);  
  28.             int i = 0;  
  29.             byte[] buffer = new byte[2048];  
  30.               
  31.             try {  
  32.                 i = input.read(buffer);  
  33.             } catch (IOException e) {  
  34.                 e.printStackTrace();  
  35.                 i = -1;  
  36.             }  
  37.             for(int j = 0;j<i;j++){  
  38.                 request.append((char)(buffer[j]));  
  39.             }  
  40.             System.out.println(request.toString());  
  41.             uri = parseUri(request.toString());  
  42.             }  
  43.     //截取请求的url  
  44.     private String parseUri(String requestString){  
  45.           
  46.         int index1 = 0;  
  47.         int index2 = 0;  
  48.         index1 = requestString.indexOf(' ');  
  49.         if(index1!=-1){  
  50.             index2 = requestString.indexOf(' ',index1+1);  
  51.             if(index2>index1){  
  52.                 return requestString.substring(index1+1,index2);  
  53.             }  
  54.         }  
  55.         return null;  
  56.     }  
  57. }  

服务器处理完得到响应数据在socket.getOutputStream();封装到Response

3、Response

[java] view plain copy
  1. public class Response {  
  2.       
  3.     private static final int BUFFER_SIZE = 1024;  
  4.     Request request;  
  5.     OutputStream output;  
  6.     public Response(OutputStream output){  
  7.         this.output = output;  
  8.     }  
  9.       
  10.     public void sendStaticResource() throws IOException{  
  11.           
  12.         byte[] bytes = new byte[BUFFER_SIZE];  
  13.         FileInputStream fis = null;  
  14.           
  15.         File file = new File(HttpServer.WEB_ROOT,request.getUri());  
  16.         if(file.exists()){  
  17.             try {  
  18.                 fis = new FileInputStream(file);  
  19.                 int ch = fis.read(bytes,0,BUFFER_SIZE);  
  20.                 while(ch != -1){  
  21.                     output.write(bytes,0,ch);  
  22.                     ch = fis.read(bytes,0,BUFFER_SIZE);  
  23.                 }  
  24.                   
  25.             } catch (FileNotFoundException e) {  
  26.                 e.printStackTrace();  
  27.             }catch(IOException e){  
  28.                 e.printStackTrace();  
  29.             }finally{  
  30.                 if(fis !=null){  
  31.                     fis.close();  
  32.                 }  
  33.             }  
  34.               
  35.         }else{  
  36.             //找不到文件  
  37.               String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +  
  38.               "Content-Type: text/html\r\n" +  
  39.               "Content-Length: 23\r\n" +  
  40.               "\r\n" +  
  41.               "<h1>File Not Found</h1>";  
  42.               try {  
  43.                 output.write(errorMessage.getBytes());  
  44.                 output.flush();  
  45.             } catch (IOException e) {  
  46.                 // TODO Auto-generated catch block  
  47.                 e.printStackTrace();  
  48.             }  
  49.         }  
  50.     }  
  51.     public Request getRequest() {  
  52.         return request;  
  53.     }  
  54.     public void setRequest(Request request) {  
  55.         this.request = request;  
  56.     }  
  57.     public OutputStream getOutput() {  
  58.         return output;  
  59.     }  
  60.     public void setOutput(OutputStream output) {  
  61.         this.output = output;  
  62.     }  
  63.     public static int getBUFFER_SIZE() {  
  64.         return BUFFER_SIZE;  
  65.     }  
  66. }