JavaFX WebView 不会加载本地主机网站
JavaFX WebView won't load localhost website
我正在尝试在 JavaFX WebView 中加载本地主机网站,但它似乎无法正常工作。我在加载网页之前在应用程序中创建了 HttpServer,但它只显示一个空白屏幕。导航到 Google Chrome 中的 http://localhost:8080/index.html 可以正常工作,所以我猜这一定是 WebView 试图加载它时出现的问题。加载其他非本地主机页面确实有效,但我无法弄清楚为什么本地主机页面出现问题。
下面的代码首先使用本地 html 文件创建 HttpServer,然后继续创建 JavaFX 阶段并将本地主机站点加载到 WebView 中,只显示一个空白页面。
public class Main extends Application {
static String path = "";
static HashMap<String, String> pages = new HashMap();
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
createPage(server, "/index.html", "index.html");
server.setExecutor(null);
server.start();
launch(args);
}
public void start(Stage stage) throws Exception {
try {
WebView webview = new WebView();
webview.getEngine().load(
"http://127.0.0.1:8080/index.html"
);
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try {
String response = readFile(pages.get(t.getHttpContext().getPath()), Charset.defaultCharset());
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
static void createPage(HttpServer server, String context, String path){
pages.put(context, path);
server.createContext(context, new MyHandler());
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
非常感谢任何试图解决此问题的帮助。谢谢!
改变t.sendResponseHeaders(200, response.length());到 t.sendResponseHeaders(200, 0);似乎解决了我的问题。
我正在尝试在 JavaFX WebView 中加载本地主机网站,但它似乎无法正常工作。我在加载网页之前在应用程序中创建了 HttpServer,但它只显示一个空白屏幕。导航到 Google Chrome 中的 http://localhost:8080/index.html 可以正常工作,所以我猜这一定是 WebView 试图加载它时出现的问题。加载其他非本地主机页面确实有效,但我无法弄清楚为什么本地主机页面出现问题。
下面的代码首先使用本地 html 文件创建 HttpServer,然后继续创建 JavaFX 阶段并将本地主机站点加载到 WebView 中,只显示一个空白页面。
public class Main extends Application {
static String path = "";
static HashMap<String, String> pages = new HashMap();
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
createPage(server, "/index.html", "index.html");
server.setExecutor(null);
server.start();
launch(args);
}
public void start(Stage stage) throws Exception {
try {
WebView webview = new WebView();
webview.getEngine().load(
"http://127.0.0.1:8080/index.html"
);
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try {
String response = readFile(pages.get(t.getHttpContext().getPath()), Charset.defaultCharset());
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
static void createPage(HttpServer server, String context, String path){
pages.put(context, path);
server.createContext(context, new MyHandler());
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
非常感谢任何试图解决此问题的帮助。谢谢!
改变t.sendResponseHeaders(200, response.length());到 t.sendResponseHeaders(200, 0);似乎解决了我的问题。