javaコマンドラインコンパイルウェブサーバー

コンパイルコマンドライン:「C:\ Program Files(x86)\ Java \ jdk1.8.0_20 \ bin \ javac.exe "* .java

コマンドを実行:java WebServer

ソースコード:

import java.io. *; 
import java.net。*; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util。*; 
 
public class WebServer { 
    public static void main(String [] args)throws IOException { 
        try { 
            // connection number 
            int i = 1; 
            //サーバーソケットを確立する
            ServerSocket s = new ServerSocket(8189); 
 
            while(true){ 
                //クライアント接続を待つ
                Socket incoming = s.accept(); 
                System.out.println( "ポートで実行中のWebServer:" + s.getLocalPort());
                System.out.println( "接続番号は次のとおりです:" + i);
                Runnable r = new ThreadHandler(incoming); 
                スレッドt =新しいThread(r); 
                t.start(); 
                i ++; 
            } 
 
        } catch(IOException e){ 
            e.printStackTrace(); 
        } 
 
    } 
} 
 
/ ** 
 *このクラスは1つのサーバーソケットのクライアント入力を処理します
 * / 
クラスThreadHandlerはRunnable { 
    private Socket incomig;を実装します。
 
    public ThreadHandler(Socket i){ 
        incomig = i; 
    } 
 
    public void run(){ 
                InputStream inStream = incomig.getInputStream(); 
        試す{
            { 
                OutputStream outStream = incomig.getOutputStream();を試してください 
                Scanner in = new Scanner(inStream); 
                PrintWriter out = new PrintWriter(outStream、true); 
 
                while(true){ 
                    String line = in.nextLine(); 
                    if(line.equals( "\ r \ n")&& line.equals( "")){ 
                        ブレーク; 
                    } 
                    System.out.println( "クライアント要求は:" +行); 
                    // read request line 
                    String [] requests = line.split( "\\ s +"); 
                    //リクエストヘッド
                    Map <String、 
                    //読み取りリクエストボディ 
                    Map <String、String> parameterMap = null; 
                    if(requests [0] .equals( "GET")){ 
                        //応答GET要求
                        // 要求が必要とするファイルを再投入します
                        String path = System.getProperties()。getProperty( "user.dir")+ requests [ 1] .replaceAll( "\\ /"、 "//"); 
                        System.out.println( "クライアントが必要とするファイルは:" +パス); 
                        //リクエストを読み取ります
                        headMap = readhead(in); 
                        //リクエストボディの読み込み
                        parameterMap = readbody(in);
                        doGet(パス、アウト);
 
                    } else if(requests [0] .equals( "POST")){ 
                        //応答POST要求
                        // 要求が要求するファイルで応答し、ファイル
                        のにパラメーターを書き込みますString path = System.getProperties()。 getProperty( "user.dir")+ requests [1] .replaceAll( "\\ /"、 "//"); 
                        System.out.println( "クライアントが必要とするファイルは:" +パス); 
                        //リクエストを読み取ります
                        headMap = readhead(in); 
                        //リクエストボディの読み込み
                        parameterMap = readbody(in);
                        doPost(path、out、parameterMap); 
 
                    } 
                } 
 
                incomig.close(); 
 
            } 
        } catch(IOException e){ 
            e.printStackTrace(); 
        } 
    } 
 
    private Map <String、String> 
        readhead(Scanner in){ String headline = in.nextLine(); 
        Map <String、String> headMap = new HashMap <>(); 
        while(!headline.equals( "\ r \ n")&&!headline.equals( "")){ 
            String [] keyValue = {""、 ""}; 
            String [] temp = headline.split( ":|:"); 
            if(temp。
                長さ== 2){ keyValue = temp; 
            }
            if(temp.length == 1){ 
                keyValue [0] = temp [0]; 
            }  
            headMap.put(keyValue [0]、keyValue [1]);
        } 
        return headMap; 
    } 
 
    private Map <String、String> readbody(Scanner in){ 
        String bodyline = in.nextLine(); 
        if(bodyline.equals( "\ r \ n")|| bodyline.equals( "")){ 
            nullを返す; 
        } 
        文字列t = in.nextLine(); 
        while(!t.equals( "\ r \ n")&&!t.equals( "")){ 
            bodyline + = t + "\ r \ n"; 
            t = in.nextLine(); 
        } 
        String []パラメータ= bodyline.split( "&"); 
        Map <String、String>
        String [] keyV = {""、 ""}; 
        for(String parameter:parameters){ 
            String [] temp = parameter。
            if(temp.length == 2){
                keyV = temp; 
            } 
            if(temp.length == 1){ 
                keyV [0] = temp [0]; 
            } 
            parameterMap.put(keyV [0]、keyV [1]); 
        } 
        return parameterMap; 
    } 
 
    private void doGet(String requestPath、PrintWriter out){ 
        final String CRLF = "\ r \ n"; 
        文字列statusLine = null; 
        文字列の日付= new Date()。toString(); 
        String contentTypeLine = ""; 
        文字列contentLengthLine = ""; 
        文字列entityBody = ""; 
 
        ファイルrequestFile = new File(requestPath); 
        パスパス= Paths.get(requestPath);
        if(requestFile.exists()){ 
            statusLine = "HTTP / 1.0 200 OK"; 
            contentLengthLine = String.valueOf(requestFile.length()); 
            { 
                contentTypeLine = Files.probeContentType(path);を試してください
                List <String>の内容= Files.readAllLines(path); 
                for(String line:contents){ 
                    entityBody + = line + CRLF; 
                } 
            } catch(IOException e){ 
                statusLine = "HTTP / 1.0 400 BadRequest"; 
                entityBody = "<HTML> 400 Not BadRequest </ TITLE>
                        "<BODY> 400 BadRequest" + 
                        e.getMessage(); 
                e.printStackTrace(); 
            } 
        } else { 
            statusLine = "HTTP / 1.0 404 Not Found"; 
            entityBody = "<HTML> 404 Not Found </ TITLE> </ HEAD>" + 
                    "<BODY> 404 Not Found"; 
 
        } 
        out.print(statusLine + CRLF); 
        out.print( "日付:" +日付+ CRLF); 
        out.print( "Content-Type:" + contentTypeLine + CRLF); 
        out.print( "Content-Length:" + contentLengthLine + CRLF); 
        out.print(CRLF); 
        out.print(entityBody + CRLF); 
        out.flush(); 
    } 
 
    private void doPost(String requestPath、PrintWriter out、
        最終文字列CRLF = "\ r \ n"; 
                contentLengthLine = String.valueOf(requestFile.length());
        文字列statusLine = null;
        文字列の日付= new Date()。toString(); 
        String contentTypeLine = ""; 
        文字列contentLengthLine = ""; 
        文字列entityBody = ""; 
 
        ファイルrequestFile = new File(requestPath); 
        パスパス= Paths.get(requestPath); 
        if(requestFile.exists()){ 
            statusLine = "HTTP / 1.0 200 OK"; 
            { 
                PrintWriter fileWrite = new PrintWriter(new FileWriter(requestFile、true));を試してください
                fileWrite.append(parameterMap.toString()); 
                fileWrite.close(); 
                contentTypeLine = Files.probeContentType(path); 
                List <String>の内容=ファイル。
                for(String line:contents){ 
                    entityBody + = line + CRLF; 
                } 
            } catch(IOException e){ 
                statusLine = "HTTP / 1.0 400 BadRequest"; 
                entityBody = "<HTML> 400 Not BadRequest </ TITLE> </ HEAD>" + 
                        "<BODY> 400 BadRequest" + 
                        e.getMessage(); 
                e.printStackTrace(); 
            } 
        } else { 
            statusLine = "HTTP / 1.0 404 Not Found";
            entityBody = "<HTML> 404 Not Found </ TITLE> </ HEAD>" + 
                    "<BODY> 404 Not Found"; 
 
        } 
        out.print(statusLine + CRLF); 
        out.print( "日付:" +日付+ CRLF); 
        out.print( "Content-Type:" + contentTypeLine + CRLF); 
        out.print( "Content-Length:" + contentLengthLine + CRLF); 
        out.print(CRLF); 
        out.print(entityBody + CRLF); 
        out.flush(); 
    } 
 
}

  

おすすめ

転載: www.cnblogs.com/coolyylu/p/12717451.html