2013-04-09 62 views
0

我在我的應用程序創建的httpserver創建的httpserver發送響應消息,使用:不能使用Java

HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); 

此服務器首先使用

OutputStream os = httpEx.getResponseBody(); 
os.write(("your request is being handled, please wait").getBytes()); 

發送響應消息,然後打開一個JFileChooser中。此外,如果有什麼差錯,將使用

os.write(("Failed to handle your request").getBytes()); 

拋出錯誤,當我的應用程序用戶的機器上運行,它創建的HttpServer正確,因爲我能找到通過日誌,但是當用戶發送到該服務器的任何請求,沒有任何反應。它顯示空白頁面。但是當我刪除所有os.write行時,它工作正常,並顯示filechooser彈出窗口。我不知道發生了什麼問題。請幫助我。 我曾嘗試這些方法:

try {   
      os.write(("Your request is being handled, please wait").getBytes()); 
      os.close(); 
      //code to open Jfilechooser pop-up 

     } catch (Exception e) { 
      os.write(("Failed to handle the request because"+e.getMessage()).getBytes()); 
      os.close();} 

try {   
       os.write(("Your request is being handled, please wait").getBytes()); 

       //code to open Jfilechooser pop-up 

      } catch (Exception e) { 
       os.write(("Failed to handle the request because"+e.getMessage()).getBytes()); 
       } 

os.close(); 

回答

0

解決它。我添加了os.flush();作爲:

finally(){ 
os.flush(); 
os.close(); 
is.close(); 
httpEx.close(); 

}