简体   繁体   中英

can not send response message from httpserver created using java

I am creating httpserver in my application, using:

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

This server first send response message using

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

and then opens one jFilechooser. also if anything goes wrong it will throw error using

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

When my application runs on user's machine, it creates httpserver properly, as I could find through logs but when user sends any request to this server, nothing happens. It displays blank page. But when I remove all os.write lines it works fine and displays the filechooser pop-up. I am not aware what is going wrong. Please help me on this. I have tried these approaches:

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

and

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

Solved it. I have added os.flush(); as:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM