-2

我正在使用套接字编程在 java 中实现 HTTP 1 协议。我得到了输出,但是在听取了一个请求后,服务器给出了如下错误

null
java.lang.NullPointerException 
at httpRequest.processRequest(httpRequest.java:24) 
at httpRequest.run(httpRequest.java:119) 
at java.lang.Thread.run(Unknown Source)

这是我的 eclipse 输出的副本

Waiting for connection

Connection Established

Waiting for connection

Connection Established

Waiting for connection

GET / HTTP/1.1
Host: localhost:5358
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

null
java.lang.NullPointerException

这是我的源代码。

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

public final class webServer
{
public static void main(String arg[]) throws Exception
{
    int port=5358;
    ServerSocket listen = new ServerSocket(port);
    while(true)
    {
        System.out.println("Waiting for connection\n\n");
        Socket server = listen.accept();
        System.out.println("Connection Established\n\n");
        httpRequest request = new httpRequest(server);
        Thread thread = new Thread(request);
        thread.start();
    }
}
}


class httpRequest implements Runnable 
{
    final String CRLF = "\r\n";
    Socket socket;
public httpRequest(Socket socket) throws Exception 
{
    this.socket = socket;
}
public void processRequest() throws Exception
{
    InputStream is = socket.getInputStream();
    DataOutputStream os = new DataOutputStream(socket.getOutputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String requestLine = br.readLine();
    System.out.println("\n\n\n" + requestLine);

    String headerLine = null;
    while ((headerLine = br.readLine()).length() != 0)
    {
        System.out.println(headerLine);
    }
    StringTokenizer tokens = new StringTokenizer(requestLine);
    tokens.nextToken(); // skip over the method, which should be "GET"
    String fileName = tokens.nextToken();
    // Prepend a "." so that file request is within the current directory.
    fileName = "." + fileName;

    FileInputStream fis = null;
    boolean fileExists = true;
    try
    {
        fis = new FileInputStream(fileName);
    } 
    catch (FileNotFoundException e)
    {
        fileExists = false;
    }
    String statusLine = null;
    String contentTypeLine = null;
    String entityBody = null;
    if (fileExists)
    {
        statusLine = "HTTP/1.0 200 OK" + CRLF ;
        contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF;
    } 
    else 
    {
        statusLine = "HTTP/1.0 404 Not Found" + CRLF;
        contentTypeLine = "Content-type: " + "text/html" + CRLF;
        entityBody = "<HTML>" + 
        "<HEAD><TITLE>Not Found</TITLE></HEAD>" +
        "<BODY>Not Found</BODY></HTML>";
    }
//  os.close();
    //br.close();
    //socket.close();
     // Send the status line.
    os.writeBytes(statusLine);

    // Send the content-type line.
    os.writeBytes(contentTypeLine);

    // Send a blank line to indicate the end of the header lines.
    os.writeBytes(CRLF);
    if (fileExists) {
         sendBytes(fis, os);
         fis.close();
        } else {
         os.writeBytes(entityBody);
        }

}
private void sendBytes(FileInputStream fis, DataOutputStream os) throws Exception {
    byte[] buffer = new byte[1024];
     int bytes = 0;
     // Copy requested file into the socket's output stream.
     while((bytes = fis.read(buffer)) != -1 ) {
     os.write(buffer, 0, bytes);

     }

}
private String contentType(String fileName)
{
    if (fileName.endsWith(".htm") || fileName.endsWith(".html"))
    {
         return "text/html";
    }
    if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))
    {
        return "image/jpeg";
    }
    if (fileName.endsWith(".gif")) 
    {
        return "image/gif";
    }
    if (fileName.endsWith(".txt"))
    {
        return "text/plain";
    }
    if (fileName.endsWith(".pdf")) 
    {
          return "application/pdf";
    }
    return "application/octet-stream";
}

public void run()
{
    try
    {
        processRequest();
    }
    catch (Exception e) 
    {
        System.out.println(e);
    }   
}
}
4

1 回答 1

0
while ((headerLine = br.readLine()).length() != 0)

上面的线是错误的, br.readLine 可以给你空值,你试图运行长度方法。它会抛出空指针。更改如下

while((headerLine = br.readLine()) != null)
于 2013-09-28T18:57:33.537 回答