2017-02-15 76 views
0

我一直在用java編寫一個簡單的HTTP Web服務器。然而,我似乎遇到了一個問題,當我嘗試獲取請求中的文件名時,我得到一個數組超出界限的錯誤,即使它沒有超出界限,我可以清楚地看到它在數組中。誰能幫助代碼如下Http webserver數組越界錯誤

public static void main(String[]args) throws Exception{ 
    ServerSocket ss = new ServerSocket(8080); 

    while(true){ 
     Socket s= ss.accept(); 
     String ip = s.getInetAddress().toString(); 
     ConnectionHandler ch = new ConnectionHandler(s); 
     ch.start(); 
     System.out.println(ip); 

    } 

public class ConnectionHandler extends Thread{ 

private Socket s; 
private PrintWriter pw; 
private BufferedReader br; 

public ConnectionHandler(Socket s) throws Exception{ 
    this.s = s; 
    br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    pw = new PrintWriter(s.getOutputStream()); 
} 

@Override 
public void run(){ 
    String reqs = ""; 

    try { 
     while(br.ready()){ 
      reqs += (char) br.read(); 
      //System.out.println(reqs); 
      HttpRequest hr = new HttpRequest(reqs); 
      HttpResponse res = new HttpResponse(hr); 
      pw.write(res.response.toCharArray()); 


     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally{ 
     pw.close(); 
     try { 
      br.close(); 
      s.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

}

public class HttpRequest { 

String fileName; 

public HttpRequest(String reqs){ 

    String lines[] = reqs.split("\n"); 

    //System.out.println(lines[0]); 
    String lines1[] = lines[0].split(" "); 
    for(int i = 0;i<lines1.length;i++){ 
     System.out.println(i + lines1[i]); 

    } 
    fileName = lines1[1]; //error is here 


} 

}

public class HttpResponse { 

private HttpRequest req; 
String response; 
private String root="H:/root"; 
private FileInputStream fis; 
private File f; 
public HttpResponse(HttpRequest hr) throws Exception { 
    req = hr; 
    if(req.fileName == null){ 
     f = new File("H:/root/helloworld.html"); 
     fis = new FileInputStream(f); 
    }else{ 
     f = new File(root + req.fileName); 
     fis = new FileInputStream(f); 
    } 


    response = "HTTP/1.1 200"; 
    response += "content-type: text/html \r\n"; 
    response += "Connection: close \r\n"; 
    response += "content-length: " + f.length() + "\r\n"; 
    response += "\r\n"; 
    int s; 
    while((s=fis.read())!=-1){ 
     response += (char) s; 
    } 
    fis.close(); 
} 

}

回答

0

的問題是: reqs += (char) br.read(); 只從輸入讀取單個字符並在每次閱讀字符後嘗試p呃請求。

所以你最終會遇到這種情況,你只有reqs中的「G」會導致行1的長度爲1. lines1[1]會嘗試訪問該數組中不存在的第二個元素。

嘗試在循環之外移動請求的解析。

[編輯]更好的格式化支持的答案。這符合我的預期。

while (br.ready()) { 
    reqs += (char) br.read(); 
} 
HttpRequest hr = new HttpRequest(reqs); 
HttpResponse res = new HttpResponse(hr); 
pw.write(res.response.toCharArray()); 
+0

試過,但得到同樣的問題。我知道它正確地讀入請求到數組中,因爲我爲每個循環做了一個檢查它的整個fileName = lines1 [1];它不喜歡,我不知道爲什麼 – jabjab25

+0

這樣做對我有用。 (編輯)我已經更新了答案,因爲代碼在這裏無法閱讀。 – Erik

+0

這對我來說非常合適,現在我知道我要去哪裏了,謝謝你的幫助 – jabjab25