Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Http Web服务器数组越界错误_Java_Http - Fatal编程技术网

Java Http Web服务器数组越界错误

Java Http Web服务器数组越界错误,java,http,Java,Http,我一直在用java开发一个简单的HTTP web服务器。然而,我似乎遇到了一个问题,当我试图获取请求中的文件名时,我得到了一个数组越界错误,即使它没有越界,而且我可以在数组中清楚地看到它。有人能帮忙吗代码在下面 public static void main(String[]args) throws Exception{ ServerSocket ss = new ServerSocket(8080); while(true){ Socket s= ss.acc

我一直在用java开发一个简单的HTTP web服务器。然而,我似乎遇到了一个问题,当我试图获取请求中的文件名时,我得到了一个数组越界错误,即使它没有越界,而且我可以在数组中清楚地看到它。有人能帮忙吗代码在下面

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();
        }

    }
}
}

}问题在于:
reqs+=(char)br.read()
只从输入中读取一个字符,在读取每个字符后,您将尝试解析请求

因此,您将在reqs中只有“G”的情况下结束,这将导致lines1的长度为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());

我试过了,但遇到了同样的问题。我知道它在请求中被正确地读入数组,因为我对每个循环都做了一次检查,检查它的整个文件名=lines1[1];它不喜欢,我不知道为什么这样对我有效。(编辑)我已经更新了答案,因为这里的代码不可读。这对我来说非常有效。现在我知道哪里出了问题。谢谢你的帮助
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();
}
while (br.ready())  {
    reqs += (char) br.read();
}
HttpRequest hr = new HttpRequest(reqs);
HttpResponse res = new HttpResponse(hr);
pw.write(res.response.toCharArray());