簡體   English   中英

無法從使用Java創建的httpserver發送響應消息

[英]can not send response message from httpserver created using java

我正在使用以下方法在應用程序中創建httpserver:

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();

解決了。 我添加了os.flush(); 如:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM