Java中的StringIndexOutOfBoundsException?

时间:2014-04-29 21:45:56

标签: java

我尝试运行这个在我的webroot文件夹中返回网页的java程序

import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class WebServer {

    static ServerSocket requestListener;
    static Socket requestHandler;
    static Scanner requestReader, pageReader;
    static DataOutputStream pageWriter;
    static String HTTPMessage;
    static String requestedFile;
    public static int HTTP_PORT = 12346;

    public static void main(String[] args) {

        try {

            requestListener = new ServerSocket(HTTP_PORT);

            System.out.println("Waiting For IE to request a page:");
            requestHandler = requestListener.accept();
            System.out.println("Page Requested: Request Header:");

            requestReader = new Scanner(new InputStreamReader(
                    requestHandler.getInputStream()));
//THis is the part where its throwing the error

            int lineCount = 0;
            do {

                lineCount++; // This will be used later
                HTTPMessage = requestReader.next();
                System.out.println(HTTPMessage);
                if (lineCount == 1) {
                    requestedFile = "WebRoot\\"
                            + HTTPMessage.substring(5,
                                    HTTPMessage.indexOf("HTTP/1.1") - 1);
                    requestedFile = requestedFile.trim();
                }

                // localhost:12346/default.htm
                // HTTPMessage = requestReader.nextLine();
                pageReader = new Scanner(new File(requestedFile));
                pageWriter = new DataOutputStream(
                        requestHandler.getOutputStream());
                while (pageReader.hasNext()) {
                    String s = pageReader.nextLine();
                    // System.out.println(s);
                    pageWriter.writeBytes(s);
                }
                // Tells the Browser we’re done sending
                pageReader.close();
                pageWriter.close();
                requestHandler.close();

            } while (HTTPMessage.length() != 0);
        } catch (Exception e) {

            System.out.println(e.toString());
            System.out.println("\n");
            e.printStackTrace();
        }
    }
}

我收到此错误消息。我应该在IE中获得一个网页,但我收到此错误消息。

等待IE请求页面:

Page Requested: Request Header:
GET
    java.lang.StringIndexOutOfBoundsException: String index out of range: -7
        at java.lang.String.substring(Unknown Source)
        at WebServer.main(WebServer.java:39)

1 个答案:

答案 0 :(得分:2)

由于String“ HTTPMessage ”不包含字符串“HTTP / 1.1”,因此抛出此错误。因此

HTTPMessage.indexOf("HTTP/1.1")  => returns -1

所以在你的子字符串函数中,这是什么传递:

 HTTPMessage.substring(5, -2);

因此错误。

要解决此错误,您应首先尝试检查HTTPMessage是否包含所需的字符串,然后尝试计算子字符串。进行以下更改:

 if (lineCount == 1 && HTTPMessage.indexOf("HTTP/1.1") != -1) {
                requestedFile = "WebRoot\\"
                        + HTTPMessage.substring(5,
                                HTTPMessage.indexOf("HTTP/1.1") - 1);
                requestedFile = requestedFile.trim();
  }