2016-05-06 16 views
0

私はHTTP応答の本体を送信する際に問題があり、私はこの行の問題だと思いますout.write(buffer, 0, bytes);私を助けてください。あなたのヘッダーが正しくOutputStreamに書かれていません私たちのサーバーからクライアントにHTTP応答を送信

DataInputStream din = new DataInputStream(ClientConn.getInputStream()); 

    OutputStream ot = ClientConn.getOutputStream(); 
    BufferedOutputStream out = new BufferedOutputStream(ot); 

    String request = din.readLine().trim(); 
    System.out.println(request); 
    StringTokenizer st = new StringTokenizer(request); 

    String header = st.nextToken(); 
    System.out.println(header); 
    if (header.equals("GET")) { 
     String fileName = st.nextToken(); 
     String file = fileName.substring(1, fileName.length()); 
     System.out.println(file); 
     FileInputStream fin = null; 
     boolean fileExist = true; 
     try { 
      fin = new FileInputStream(file); 
     } 
     catch (Exception ex) { 
      fileExist = false; 
     } 

     String ServerLine = "Simple HTTP Server"; 
     String StatusLine = null; 
     String ContentTypeLine = null; 
     String ContentLengthLine = null; 
     String ContentBody = null; 

     if (fileExist) { 
      StatusLine = "HTTP/1.0 200 OK"; 
      ContentTypeLine = "Content-type: text/html"; 
      ContentLengthLine = "Content-Length: " + (new Integer(fin.available()).toString()); 
     } else { 
      StatusLine = "HTTP/1.0 200 OK"; 
      ContentTypeLine = "Content-type: text/html"; 
      ContentBody = "<HTML>" + 
        "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" + 
        "<BODY>404 Not Found" + 
        "</BODY></HTML>"; 
      ContentLengthLine = (new Integer(ContentBody.length()).toString()); 
     } 

     out.write(StatusLine.getBytes()); 
     out.write(ServerLine.getBytes()); 
     out.write(ContentTypeLine.getBytes()); 
     out.write(ContentLengthLine.getBytes()); 
     // output.writeUTF(file); 
     if (fileExist) { 

      byte[] buffer = new byte[1024]; 
      int bytes = 0; 
      while ((bytes = fin.read(buffer)) != -1) { 
       out.write(buffer, 0, bytes); 

       for (int iCount = 0; iCount < bytes; iCount++) { 
        int temp = buffer[iCount]; 
        System.out.print((char) temp); 
       } 
      } 

     } 

     out.flush(); 
     fin.close(); 
    } else { 
     //out.write(ContentBody.getBytes()); 
    } 

    out.close(); 
    ClientConn.close(); 
+0

例外が発生していますか?メッセージは何ですか? –

答えて

0

、あなたは、各行の終わりに"\r\n"あるEOL文字を書くことを忘れています。あなたの体の内容を書き始める前に、EOL文字も書く必要があります。言い換えれば

は、あなたがこのような何かをする必要があります。

String eol = "\r\n"; 
Charset charset = Charset.forName("ASCII"); 
byte[] eolBytes = eol.getBytes(charset); 
out.write(StatusLine.getBytes(charset)); 
out.write(eolBytes); 
out.write(ServerLine.getBytes(charset)); 
out.write(eolBytes); 
out.write(ContentTypeLine.getBytes(charset)); 
out.write(eolBytes); 
out.write(ContentLengthLine.getBytes(charset)); 
out.write(eolBytes); 
// End of the header 
out.write(eolBytes); 
// Here the body begin 

は確かにあなたのヘッダーは ASCIIにエンコードする必要があります。

レスポンス更新:あなたのコードに関する

その他の発言:

  1. ファイルが存在するかどうかを確認する方法File#exists()を使用してください。
  2. ファイルが存在しない場合は、現在のコードに本文を書き込まないだけです。ファイルのサイズを取得するには、Files.getAttribute(Paths.get("/path/to/my/file"), "size")を使用してください。
+0

ありがとう、それは正常に実行されます –

+0

@FlonaKroso良いニュース! –

+0

私たちは、404エラーをテストするためのURLに存在しない書き込みファイルを試してみますが、私にこのエラーを与えるでしょう例外 "メイン" java.lang.NullPointerExceptionの例外 \t http_server.HttpRequest.process(HttpRequest.java:126) \t http_server.Http_server.main(Http_server.java:25) Javaの結果:1 –

関連する問題