tcp4http

来源:互联网 发布:editplus怎么用java 编辑:程序博客网 时间:2024/05/18 01:20

package tcp.http.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class SimpleHttpServer
{
private String encoding = “UTF-8”;

private String line_separater = "\r\n";public SimpleHttpServer(){    try    {        @SuppressWarnings("resource")        ServerSocket serverSocket = new ServerSocket(8080);        while (true)        {            Socket client = serverSocket.accept();            new Thread(new ServerWorker(client)).start();        }    } catch (Exception e)    {        e.printStackTrace();    }}class ServerWorker implements Runnable{    private Socket client;    public ServerWorker(Socket client)    {        super();        this.client = client;    }    public void run()    {        handleRequest(client);    }}public void handleRequest(Socket client){    if (null == client)    {        return;    }    try    {        InputStream is = client.getInputStream();        String line = readLine(is);        String method = new StringTokenizer(line).nextElement().toString();        int contentLength = 0;        StringBuilder builder = new StringBuilder();        do        {            builder.append(line);            line = readLine(is);            if (line.startsWith("Content-Length"))            {                contentLength = Integer.parseInt(line.split(":")[1].trim());            }        } while (!line.equals(line_separater));        builder.append(line);        if ("POST".equalsIgnoreCase(method))        {            builder.append(readBody(is, contentLength));        }        System.out.println(builder.toString());        printResponse(client);        closeSocket(client);    } catch (Exception e)    {        e.printStackTrace();    }}private void printResponse(Socket client) throws IOException{    PrintWriter out = new PrintWriter(client.getOutputStream(), true);    out.println("HTTP/1.0 200 OK");    out.println("Content-Type:text/html;charset=" + encoding);    out.println();    out.println("<h1> Hello Http Server</h1>");    out.close();}private void closeSocket(Socket socket){    try    {        socket.close();    } catch (IOException ex)    {        ex.printStackTrace();    }}private String readBody(InputStream is, int contentLength)        throws IOException{    List<Byte> bytes = new ArrayList<Byte>();    byte b = 0;    int total = 0;    do    {        b = (byte) is.read();        bytes.add(Byte.valueOf(b));        total++;    } while (total < contentLength);    return new String(byteList2Arr(bytes), encoding);}private String readLine(InputStream is) throws IOException{    List<Byte> bytes = new ArrayList<Byte>();    byte b = 0;    do    {        b = (byte) is.read();        bytes.add(Byte.valueOf(b));    } while (10 != b);    return new String(byteList2Arr(bytes), encoding);}private byte[] byteList2Arr(List<Byte> bytes){    byte[] byteArr = new byte[bytes.size()];    for (int i = 0; i < bytes.size(); i++)    {        byteArr[i] = ((Byte) bytes.get(i)).byteValue();    }    bytes.clear();    return byteArr;}public static void main(String[] args){    new SimpleHttpServer();}

}

0 0