2012-03-16 99 views
0

我試圖運行一個簡單的多線程服務器,它提取一個URL,並且允許瀏覽器上傳文件到服務器(GET和POST)它使用GET獲取網頁對於POST有問題,這裏是我用於上傳工作的WebServer。注:HttpRequest的是另一個類來處理線程多線程http服務器從客戶端瀏覽器採取GET和POST

import java.net.*; 
import java.io.*; 


public class WebServer 
{ 

    public WebServer(int port) 
    { 

     System.out.println("starting web server on port " + port); 

     ServerSocket serverSocket = null; 

     try 
     { 
      //create the server 
      serverSocket = new ServerSocket(port); 
     }catch(IOException ex) 
     { 
      System.out.println("could not open port " + port); 
      System.exit(1); 
     } 

     //loop indefinitely 
     while(true) 
     { 
      try 
      { 
       Socket connection = null; 
       connection = serverSocket.accept(); 
       //accept the connection 

       //create a new thread, start it and return to the waiting state 
       HttpRequest request = new HttpRequest(connection); 
       Thread t = new Thread(request); 
       t.start(); 

      }catch(IOException ex) 
      { 
       //fail if an error occurs 
       System.out.println("problem accepting connection"); 
       System.exit(1); 
      } 
     } 

    } 

    public static void main(String[] args) 
    { 

     //simple validation for the port number 
     if(args.length != 1) 
     { 
      System.out.println("please specify a port"); 
      System.exit(1); 
     } 

     int port = -1; 
     try 
     { 
      port = Integer.parseInt(args[0]); 

     }catch(NumberFormatException ex) 
     { 
      System.out.println("invalid port number"); 
      System.exit(1); 
     } 

     WebServer server = new WebServer (port); 

    } 

} 

這裏是HTTP實現Runnable

import java.net.*; 
import java.io.*; 

public class HttpRequest implements Runnable 
{ 

    private DataInputStream input = null; 
    private Socket connection; 

    private static DataOutputStream output = null; 

    public HttpRequest(Socket connection) 
    { 
     this.connection = connection; 
    } 

    //required method so this can be used as a thread 
    public void run() 
    { 
     try 
     { 
      //try and get the streams 
      input = new DataInputStream(connection.getInputStream()); 
      output = new DataOutputStream(connection.getOutputStream()); 
     } 
     catch(IOException ex) 
     { 
      System.out.println("could not get input/output streams from connection: " + connection.toString()); 
      return; 
     } 

     try 
     { 
      StringBuilder response = new StringBuilder(""); 

      String request = input.readLine(); 

      System.out.println("request: " + request); 

      String[] requestArray = request.split(" "); 

      //read off and ignore the rest of the input 
      //added so this can be tested with real browsers 
      while(input.available() != 0) 
      { 
       input.read(); 
      } 

      if (requestArray.length != 3) 
      { 
       //request should be of the format GET /index.html HTTP/1.1, die if a bad request comes in 
       System.out.println("bad request: " + request); 
       return; 
      }else 
      { 
       //requested file should be the second entry, remove the leading '/' 
       File requestedFile = new File(requestArray[1].substring(1)); 

       System.out.println("requested file: " + requestedFile); 

       //check the requested file exists 
       if(requestedFile.exists()) 
       { 
        System.out.println("file found, sending response"); 

        DataInputStream fileInput = new DataInputStream(new FileInputStream(requestedFile)); 

        //output HTTP header, must be followed by two new lines 
        response.append("HTTP/1.1 200 OK\n\n"); 

        String line = fileInput.readLine(); 

        while(line != null) 
        { 
         response.append(line); 
         line = fileInput.readLine(); 
        } 

        fileInput.close(); 

        output.writeBytes(response.toString()); 
        output.flush(); 
        output.close(); 

        Logger.writeToLog("Request: " + request + "\r\nResponse: " + response.toString()); 

       } 
       else 
       { 
        System.out.println("file not found, sending 404"); 

        response.append("HTTP/1.1 404 Not Found\n\n"); 
        output.writeBytes(response.toString()); 

        output.flush(); 
        output.close(); 
       } 

      } 

     } 
     catch(IOException ex) 
     { 
      System.out.println("cannot read request from: " + connection.toString() + ex.toString()); 
      return; 
     } 
     catch(NullPointerException ex) 
     { 
      System.out.println("bad request: " + connection.toString()); 
      return; 
     } 

     try 
     { 
      input.close(); 
      output.close(); 

      connection.close(); 
     } 
     catch(IOException ex) 
     { 
      System.out.println("Can't close connection: " + connection.toString()); 
      return; 
     } 
    } 

} 
+1

你能提供一些比'iam對POST有問題'更詳細的信息嗎?當客戶發出POST請求時,你會看到什麼? – Quantum7 2012-03-16 02:16:02

+0

它迴應一個404文件沒有找到,就好像我要求一個GET – ruth542 2012-03-16 18:26:56

回答

2

你的問題是,你的HttpRequest類不正確執行HTTP協議。對於初學者,您假定所有請求都是GET請求,並且您忽略了請求行後面的標題行。

你需要做的是閱讀HTTP 1.1規範......徹底......重寫你的代碼,以便它讀取並處理請求,並根據規範說明應該如何完成生成響應。

另外,不要浪費你的時間重新發明輪子(可能不正確)。使用現有的Web容器框架或現有的HTTP協議棧,如Apache HttpComponents。

+0

我該如何修改它以考慮POST請求? – ruth542 2012-03-16 18:58:14

相關問題