Note: Advice in this article will only work for JxBrowser 6. See the corresponding guide and example for JxBrowser 7.(注意:本文中的建议仅适用于JxBrowser6。请参阅相应的指南和JxBrowser 7示例。)


JxBrowser allows loading HTML by URL, from local HTML file, from a String. Very often Java application resources such as HTML files are located inside JAR archives included into application class path. To be able to load resources located inside JAR archive you must register custom ProtocolHandler with the following implementation:(JxBrowser允许通过URL,本地HTML文件或字符串加载HTML。通常,Java应用程序资源(例如HTML文件)位于应用程序类路径中包含的JAR归档文件中。为了能够加载位于JAR存档内的资源,必须使用以下实现注册自定义ProtocolHandler:)

BrowserContext browserContext = browser.getContext();
ProtocolService protocolService = browserContext.getProtocolService();
protocolService.setProtocolHandler("jar", new ProtocolHandler() {
    @Override
    public URLResponse onRequest(URLRequest request) {
        try {
            URLResponse response = new URLResponse();
            URL path = new URL(request.getURL());
            InputStream inputStream = path.openStream();
            DataInputStream stream = new DataInputStream(inputStream);
            byte[] data = new byte[stream.available()];
            stream.readFully(data);
            response.setData(data);
            String mimeType = getMimeType(path.toString());
            response.getHeaders().setHeader("Content-Type", mimeType);
            return response;
        } catch (Exception ignored) {}
        return null;
    }
});

The getMimeType() method returns appropriate mime type for the given resource extension:(getMimeType()方法为给定的资源扩展返回合适的mime类型:)

private static String getMimeType(String path) {
    if (path.endsWith(".html")) {
        return "text/html";
    }
    if (path.endsWith(".css")) {
        return "text/css";
    }
    if (path.endsWith(".js")) {
        return "text/javascript";
    }
    return "text/html";
}

You can extend this method with additional extensions and mime types.(您可以使用其他扩展名和mime类型来扩展此方法。)


Once you register ProtocolHandler and define what mime types are supported, you can load resources from JAR archive using standard Java and JxBrowser API. For example:(一旦注册了ProtocolHandler并定义了支持的mime类型,就可以使用标准Java和JxBrowser API从JAR存档中加载资源。例如:)

browser.loadURL(getClass().getResource("index.html").toString());

Example(例)

The complete example you can find below:(您可以在下面找到完整的示例:)

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;

public class ProtocolHandlerSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        BrowserContext browserContext = browser.getContext();
        ProtocolService protocolService = browserContext.getProtocolService();
        protocolService.setProtocolHandler("jar", new ProtocolHandler() {
            @Override
            public URLResponse onRequest(URLRequest request) {
                try {
                    URLResponse response = new URLResponse();
                    URL path = new URL(request.getURL());
                    InputStream inputStream = path.openStream();
                    DataInputStream stream = new DataInputStream(inputStream);
                    byte[] data = new byte[stream.available()];
                    stream.readFully(data);
                    response.setData(data);
                    String mimeType = getMimeType(path.toString());
                    response.getHeaders().setHeader("Content-Type", mimeType);
                    return response;
                } catch (Exception ignored) {}
                return null;
            }
        });

        // Assume that we need to load a resource related to this class in the JAR file
        browser.loadURL(ProtocolHandlerSample.class.getResource("index.html").toString());
    }

    private static String getMimeType(String path) {
        if (path.endsWith(".html")) {
            return "text/html";
        }
        if (path.endsWith(".css")) {
            return "text/css";
        }
        if (path.endsWith(".js")) {
            return "text/javascript";
        }
        return "text/html";
    }
}