2016-08-10 3 views
-2

基本的には着信パケットを読み取るJavaサーバーを作成しましたが、実際のデータを含む本文部分を読み取らないポストパケットが来ると問題が発生しますメッセージはPOST /index.html http/1.1となります。ここでポストパケットの本文をJavaで読み取る方法

は、私は自分のサーバー上のデータを読み込む方法です:

try{ 
    IR = new InputStreamReader(socket.getInputStream()); 
}catch(Exception ie){ 
    System.out.println("Cound'nt create IR"); 
} 

BufferedReader BR = new BufferedReader(IR); 
String MESSAGE = null; 
try{ 
    MESSAGE = BR.readLine(); 
}catch(Exception ie){ 
    System.out.println("Cound'nt Receive Message"); 
} 
System.out.println(MESSAGE); 

あなたはどのように受信したパケットのボディを読み取ることを教えていただけますか?

+0

どのJavaのバージョンを使用しますか? –

答えて

0

BR.readLine()は最初の行のみを返します。すべての行を読むには、BufferedReaderをループする必要があります。可能な方法の1つは、

String fullMessage; 
String aux; 
do{ 
    aux = BR.readLine(); 
    fullMessage = fullMessage.concat(aux); 
}while(aux != null); 

です。これは単なる例です。

ドキュメントを読む:Java用https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

例7+あなたが使用できるJavaの古いバージョンの

String message = null; 
    //Try with resources, java will handle the closing of the stream, event if exception is thrown. 
    try ( InputStreamReader inputStream = new InputStreamReader(socket.getInputStream()); 
      BufferedReader bufferReader = new BufferedReader(inputStream);) { 

     String aux = null; 
     do { 
      aux = bufferReader.readLine(); 
      message = message.concat(aux); 
     } while (aux != null); 

    } catch (IOException e) { 
     System.out.println("Failed to read input stream from socket"); 
    } 

    System.out.println("Message: " + message); 

InputStreamReader inputStream = null; 
    BufferedReader bufferReader = null; 
    String message = null; 
    try { 
     inputStream = new InputStreamReader(socket.getInputStream()); 
     bufferReader = new BufferedReader(inputStream); 
     String aux = null; 
     do { 
      aux = bufferReader.readLine(); 
      message = message.concat(aux); 
     } while (aux != null); 

     inputStream.close(); 
     bufferReader.close(); 
    } catch (IOException e) { 
     //Read throws IOException, don't just use Exception (this could hide other exceptions that you are not treating). 
     System.out.println("Failed to read input stream from socket"); 
    } finally{ 
     //Use 2 try-catch, if you only use one and the first fails, the second will never close. 
     try{inputStream.close();}catch(IOException ioe){} 
     try{bufferReader.close();}catch(IOException ioe){} 
    } 


    System.out.println("Message: "+message); 

編集:申し訳ありませんがAPPENDはStringBufferをとStringBuilderのためのものですが、私の悪い申し訳ありません。

EDIT2:2つの例を追加しました。

+0

それはappend()が定義されていないと言いますが、 – rocky

+0

あなたのアイデアはうまくいきますが、決してループから出ることはありません。 – rocky

+0

私はあなたがストリームを閉じていないという問題があると思います。私は2番目に答えを更新します。 – gabun88

1

は、Java 8を使用する場合は

List<String> content = BR.lines().collect(Collectors.toList()); 

を行うことができますそれ以外の場合は、あなたが全体の応答を取得したいとその後/フォーマットを解析

List<String> content = new ArrayList<>(); 
String line; 
while((line = BR.readLine()) != null) { 
    content.add(line); 
} 

を行うことができます。

+0

あなたの答えはうまくいきましたが、今は決してループから抜け出せません。リストコンテンツ=新しいArrayList <>(); \t \t \t文字列; \t \t \t試み{ \t \t \t \t一方((ライン= BR.readLine())!= NULL){ \t \t \t \t content.add(ライン) \t \t \t \t} \t \t \t}キャッチ(のIOExceptionのE1){ \t \t \t \t E1。printStackTrace(); { \t \t \t \tのSystem.out.println(content.get(I))(; iが(content.sizeを<)I ++は、I = 0の整数)ため \t \t \t} \t \t \t \t \t \t。 \t \t \t} "私はarraylistを印刷するためのforループを作成していますが、私が要求するブラウザではロードが滞っています。から。 – rocky

関連する問題