Java Http 프로 토 콜 서버 데모

import java.io.*;  
import java.net.*;  
/** 
 * MyHttpServer        HTTP    ,            
 *       response 
 *        , http          
 * **/  
public class MyHttpServer {  
    //      ,post.html, upload.html        
    public static String WEB_ROOT = "c:/root";  
    //    
    private int port;  
    //        url  
    private String requestPath;  
    //mltipart/form-data    post    ,   
    private String boundary = null;  
    //post            
    private int contentLength = 0;  
  
    public MyHttpServer(String root, int port) {  
        WEB_ROOT = root;  
        this.port = port;  
        requestPath = null;  
    }  
    //  GET    
    private void doGet(DataInputStream reader, OutputStream out) throws Exception {  
        if (new File(WEB_ROOT + this.requestPath).exists()) {  
            //                          
            InputStream fileIn = new FileInputStream(WEB_ROOT + this.requestPath);  
            byte[] buf = new byte[fileIn.available()];  
            fileIn.read(buf);  
            out.write(buf);  
            out.close();  
            fileIn.close();  
            reader.close();  
            System.out.println("request complete.");  
        }  
    }  
    //  post    
    private void doPost(DataInputStream reader, OutputStream out) throws Exception {  
        String line = reader.readLine();  
        while (line != null) {  
            System.out.println(line);  
            line = reader.readLine();  
            if ("".equals(line)) {  
                break;  
            } else if (line.indexOf("Content-Length") != -1) {  
                this.contentLength = Integer.parseInt(line.substring(line.indexOf("Content-Length") + 16));  
            }  
            //       ,    doMultiPart  。  
            else if(line.indexOf("multipart/form-data")!= -1){  
                // multiltipart      
                this.boundary = line.substring(line.indexOf("boundary") + 9);  
                this.doMultiPart(reader, out);  
                return;  
            }  
        }  
        //      post(    )       
        System.out.println("begin reading posted data......");  
        String dataLine = null;  
        //     post      
        byte[] buf = {};  
        int size = 0;  
        if (this.contentLength != 0) {  
            buf = new byte[this.contentLength];  
            while(size<this.contentLength){  
                int c = reader.read();  
                buf[size++] = (byte)c;  
                  
            }  
            System.out.println("The data user posted: " + new String(buf, 0, size));  
        }  
        //           
        String response = "";  
        response += "HTTP/1.1 200 OK/n";  
        response += "Server: Sunpache 1.0/n";  
        response += "Content-Type: text/html/n";  
        response += "Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT/n";  
        response += "Accept-ranges: bytes";  
        response += "/n";  
        String body = "<html><head><title>test server</title></head><body><p>post ok:</p>" + new String(buf, 0, size) + "</body></html>";  
        System.out.println(body);  
        out.write(response.getBytes());  
        out.write(body.getBytes());  
        out.flush();  
        reader.close();  
        out.close();  
        System.out.println("request complete.");  
    }  
    //      
    private void doMultiPart(DataInputStream reader, OutputStream out) throws Exception {  
        System.out.println("doMultiPart ......");  
        String line = reader.readLine();  
        while (line != null) {  
            System.out.println(line);  
            line = reader.readLine();  
            if ("".equals(line)) {  
                break;  
            } else if (line.indexOf("Content-Length") != -1) {  
                this.contentLength = Integer.parseInt(line.substring(line.indexOf("Content-Length") + 16));  
                System.out.println("contentLength: " + this.contentLength);  
            } else if (line.indexOf("boundary") != -1) {  
                //  multipart     
                this.boundary = line.substring(line.indexOf("boundary") + 9);  
            }  
        }  
        System.out.println("begin get data......");  
        /*                      ,            ***** 
        <HTTP     > 
        ............ 
        Cache-Control: no-cache 
        <       ,                > 
        -----------------------------7d925134501f6<  multipart   > 
        Content-Disposition: form-data; name="myfile"; filename="mywork.doc" 
        Content-Type: text/plain 
         
        <    >........................................ 
        ................................................. 
         
        -----------------------------7d925134501f6<  multipart   > 
        Content-Disposition: form-data; name="myname"<       > 
        <       > 
        <          > 
        -----------------------------7d925134501f6--<  multipart   ,          -> 
        ****************************************************************/  
        /** 
         *             multipart   POST     , 
         *        ,                    
         * **/  
        if (this.contentLength != 0) {  
            //         ,             buf.  
            byte[] buf = new byte[this.contentLength];  
            int totalRead = 0;  
            int size = 0;  
            while (totalRead < this.contentLength) {  
                size = reader.read(buf, totalRead, this.contentLength - totalRead);  
                totalRead += size;  
            }  
            // buf       ,                     
            String dataString = new String(buf, 0, totalRead);  
            System.out.println("the data user posted:/n" + dataString);  
            int pos = dataString.indexOf(boundary);  
            //    4             
            pos = dataString.indexOf("/n", pos) + 1;  
            pos = dataString.indexOf("/n", pos) + 1;  
            pos = dataString.indexOf("/n", pos) + 1;  
            pos = dataString.indexOf("/n", pos) + 1;  
            //        
            int start = dataString.substring(0, pos).getBytes().length;  
            pos = dataString.indexOf(boundary, pos) - 4;  
            //        
            int end = dataString.substring(0, pos).getBytes().length;  
            //    filename  
            int fileNameBegin = dataString.indexOf("filename") + 10;  
            int fileNameEnd = dataString.indexOf("/n", fileNameBegin);  
            String fileName = dataString.substring(fileNameBegin, fileNameEnd);  
            /** 
             *                   ,  c:/my file/somedir/project.doc 
             *             ,  myphoto.jpg. 
             *          。 
            */  
            if(fileName.lastIndexOf("//")!=-1){  
                fileName = fileName.substring(fileName.lastIndexOf("//") + 1);  
            }  
            fileName = fileName.substring(0, fileName.length()-2);  
            OutputStream fileOut = new FileOutputStream("c://" + fileName);  
            fileOut.write(buf, start, end-start);  
            fileOut.close();  
            fileOut.close();  
        }  
        String response = "";  
        response += "HTTP/1.1 200 OK/n";  
        response += "Server: Sunpache 1.0/n";  
        response += "Content-Type: text/html/n";  
        response += "Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT/n";  
        response += "Accept-ranges: bytes";  
        response += "/n";  
        out.write("<html><head><title>test server</title></head><body><p>Post is ok</p></body></html>".getBytes());  
        out.flush();  
        reader.close();  
        System.out.println("request complete.");  
    }  
  
    public void service() throws Exception {  
        ServerSocket serverSocket = new ServerSocket(this.port);  
        System.out.println("server is ok.");  
        //  serverSocket        ,              
        //       GET POST      
        //  POST             
        while (true) {  
            Socket socket = serverSocket.accept();  
            System.out.println(socket.getInetAddress());
            System.out.println("new request coming.");  
            DataInputStream reader = new DataInputStream((socket.getInputStream()));  
            String line = reader.readLine();  
            String method = line.substring(0, 4).trim();  
            OutputStream out = socket.getOutputStream();  
            this.requestPath = line.split(" ")[1];  
            System.out.println(method);  
            if ("GET".equalsIgnoreCase(method)) {  
                System.out.println("do get......");  
                this.doGet(reader, out);  
            } else if ("POST".equalsIgnoreCase(method)) {  
                System.out.println("do post......");  
                this.doPost(reader, out);  
            }  
            socket.close();  
            System.out.println("socket closed.");  
        }  
    }  
    public static void main(String args[]) throws Exception {  
        MyHttpServer server = new MyHttpServer("c:/root", 8080);  
        server.service();  
    }  
}  

좋은 웹페이지 즐겨찾기