-2

ソケットプログラミングを使用してJavaでHTTP 1プロトコルを実装しています。出力を取得しましたが、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 は、length メソッドを実行しようとしている null 値を与える可能性があります。nullポインターをスローします。以下のように変更します

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