无法从使用java创建的httpserver发送响应消息
我正在应用程序中创建httpserver,使用:无法从使用java创建的httpserver发送响应消息,java,httpresponse,httpserver,Java,Httpresponse,Httpserver,我正在应用程序中创建httpserver,使用: HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); 此服务器首先使用发送响应消息 OutputStream os = httpEx.getResponseBody(); os.write(("your request is being handled, please wait").getBytes()); 然后打开一个jFilechooser。此外,如果
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
行时,它工作正常,并显示文件选择器弹出窗口。我不知道出了什么问题。请帮我做这个。
我尝试过以下方法:
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();}
及
解决了。我添加了os.flush(); 作为:
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();
finally(){
os.flush();
os.close();
is.close();
httpEx.close();
}