1

I'm writing a localhost web/websocket application bundled inside an uber jar.

It's a single-page site, and the HttpServlet will send the html that communicates with the WebSocket server. I'd like the page to remain inside the jar and have the HttpServlet be able to load it from there and send it to the client.

Is this possible? If so, how?

4

2 回答 2

0

目前尚不清楚您的要求是什么。

如果您尝试从类路径加载文件(例如在 jar 中),您可以这样做

public class Test {

    public static void main(String[] args) {
        InputStream resourceAsStream = Test.class.getResourceAsStream("/test.html");
        // use the stream here...
        System.out.println(resourceAsStream);
    }
}
于 2014-01-13T21:27:43.000 回答
0

HttpServlet 可以返回它想要的任何东西,你需要做的就是在响应中设置你想要的东西。

我猜你实际上正在寻找的答案看起来像这样

public class MyServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out = response.getWriter();
        InputStream input = this.class.getResourceAsStream("/path/to/this.html");
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line;
        while ((line = reader.readLine()) != null) {
            out.println(line);
        }
    }
}
于 2014-01-13T21:26:37.327 回答