首页

三个类文件实现一个的超简单HTTP服务器之完整Java源码下载

标签:java源码,文件服务器,nginx,apache,JAVA,serverSocket,socket,http,web服务器,HttpServer     发布时间:2016-04-22   
  • 云盘下载:
  • [提取码:0000]
  • 本地下载:
       ( 需积分:2  )

一、源码简介

本源码是用Java语言实现的一个超简单HTTP服务器项目,帮助同学直观了解构建一个服务器的要素及原理。该项目主要有包含三个核心类文件HttpServer.java-服务器实体类、请求类-Request.java、响应类-Response.java,具体源码参见GitHub地址:https://github.com/xwooduser/HttpServerDemo

二、项目快照

三个类文件实现一个的超简单HTTP服务器之完整Java源码下载

三个类文件实现一个的超简单HTTP服务器之完整Java源码下载

三、源码类

HttpServer类

import java.net.Socket;@b@import java.net.ServerSocket;@b@import java.net.InetAddress;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.IOException;@b@import java.io.File;@b@@b@public class HttpServer {@b@@b@    public static final String WEB_ROOT = System.getProperty("user.dir")@b@            + File.separator + "webroot";@b@@b@    private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";@b@@b@    private boolean shutdown = false;@b@@b@    public static void main(String[] args) {@b@        HttpServer server = new HttpServer();@b@        server.await();@b@    }@b@@b@    public void await() {@b@        ServerSocket serverSocket = null;@b@        int port = 9999;@b@        try {@b@            serverSocket = new ServerSocket(port, 1,@b@                    InetAddress.getByName("127.0.0.1"));@b@        } catch (IOException e) {@b@            e.printStackTrace();@b@            System.exit(1);@b@        }@b@@b@        while (!shutdown) {@b@            Socket socket = null;@b@            InputStream input = null;@b@            OutputStream output = null;@b@            System.out.println("Http Server is started@thread:"+Thread.currentThread());@b@            try {@b@                socket = serverSocket.accept();@b@                input = socket.getInputStream();@b@                output = socket.getOutputStream();@b@@b@                Request request = new Request(input);@b@                request.parse();@b@@b@                Response response = new Response(output);@b@                response.setRequest(request);@b@                response.sendStaticResource();@b@@b@                socket.close();@b@@b@                shutdown = request.getUri().equals(SHUTDOWN_COMMAND);@b@            } catch (Exception e) {@b@                e.printStackTrace();@b@                continue;@b@            }@b@        }@b@    }@b@}

Request类

import java.io.InputStream;@b@import java.io.IOException;@b@@b@public class Request {@b@@b@    private InputStream input;@b@    private String uri;@b@@b@    public Request(InputStream input) {@b@        this.input = input;@b@    }@b@@b@    public void parse() {@b@@b@        StringBuffer request = new StringBuffer(2048);@b@        int i;@b@        byte[] buffer = new byte[2048];@b@        try {@b@            i = input.read(buffer);@b@        } catch (IOException e) {@b@            e.printStackTrace();@b@            i = -1;@b@        }@b@        for (int j = 0; j < i; j++) {@b@            request.append((char) buffer[j]);@b@        }@b@        System.out.print("$$"+request.toString());@b@        uri = parseUri(request.toString());@b@        System.out.println("uri="+uri);@b@    }@b@@b@    private String parseUri(String requestString) {@b@        int index1, index2;@b@        index1 = requestString.indexOf(' ');@b@        if (index1 != -1) {@b@            index2 = requestString.indexOf(' ', index1 + 1);@b@            if (index2 > index1)@b@                return requestString.substring(index1 + 1, index2);@b@        }@b@        return null;@b@    }@b@@b@    public String getUri() {@b@        return uri;@b@    }@b@@b@}

Response类

import java.io.OutputStream;import java.io.IOException;@b@import java.io.FileInputStream;@b@import java.io.File;@b@@b@public class Response {@b@@b@    private static final int BUFFER_SIZE = 1024;@b@    Request request;@b@    OutputStream output;@b@@b@    public Response(OutputStream output) {@b@        this.output = output;@b@    }@b@@b@    public void setRequest(Request request) {@b@        this.request = request;@b@    }@b@@b@    public void sendStaticResource() throws IOException {@b@        byte[] bytes = new byte[BUFFER_SIZE];@b@        FileInputStream fis = null;@b@        try {@b@            File file = new File(HttpServer.WEB_ROOT, request.getUri());@b@            if (file.exists()) {@b@                fis = new FileInputStream(file);@b@                int ch = fis.read(bytes, 0, BUFFER_SIZE);@b@                while (ch != -1) {@b@                    output.write(bytes, 0, ch);@b@                    System.out.println("##"+new String(bytes));@b@                    ch = fis.read(bytes, 0, BUFFER_SIZE);@b@                }@b@            } else {@b@                @b@                String errorMessage = "HTTP/1.1 404 File Not Found@b@"@b@                        + "Content-Type: text/html@b@"@b@                        + "Content-Length: 23@b@" + "@b@"@b@                        + "<h1>File Not Found</h1>";@b@                System.out.println("errorMessage"+errorMessage);@b@                output.write(errorMessage.getBytes());@b@            }@b@        } catch (Exception e) {            @b@            System.out.println(e.toString());@b@        } finally {@b@            if (fis != null)@b@                fis.close();@b@        }@b@    }@b@}