0
我正在使用Java的簡單網絡服務器應用程序。一切都可以,但是當http響應或HTML文檔發送到瀏覽器(如Firefox或Chrome瀏覽器)時,在輸出流或套接字關閉之前不顯示任何內容。有沒有辦法強制瀏覽器顯示結果而不關閉流? 或者在我的代碼中有任何問題?瀏覽器不顯示響應,直到關閉它的流
請注意,http響應頭和它的正文是正確的(content-lenght),但瀏覽器仍在加載並且從不顯示任何內容。
public void run()
{
byte [] recbuf = new byte[16384]; //buffer 16KB
int i = 0;
try
{
while(CatServerSocket.isConnected())
{
i = CatServerSocket.getInputStream().read(recbuf,0,16384); //here
if(i<0)break;
clientSocket.getOutputStream().write(recbuf,0,i);
clientSocket.getOutputStream().flush();
//if socket or output stream closed here, browser shows results
System.out.println("INFO: " + i + " bytes recieved from CatServer!");
System.out.println((new String(recbuf,0,i)));
}
System.out.println("ERROR: Connection to CatServer lost!");
}
catch(IOException e){e.printStackTrace();}
}