Refer to a web page inside a jar file

社会主义新天地 提交于 2019-12-24 16:35:44

问题


I have a working project. It includes another project which deals with REST as a rest-api.jar.

Now I have done an one-page UI for this REST API on AngularJS and provided it for the main app.

The problem is that I cannot find a way to refer to this page from the main project because this page MUST be within the rest_api.jar

This is a way I solved it. Simply redirecting to a page:

@GET
@Produces(MediaType.TEXT_HTML)
public void doGetAsHtml(@Context
UriInfo info, @Context
HttpServletResponse servletResponse) throws IOException
{
    servletResponse.sendRedirect("/web/pages/index.html");
}

So, is it possible to refer to a web page that is inside one of your project jar files and how this can be achieved?

Every answer is highly appreciated and responded immidiately.

Thank you.


回答1:


It depends what you mean by 'refer to'.

You can open a file that is on your classpath (e.g. in a jar file used by your application) using Class.getResourceAsStream() and passing it the path e.g. '/resource/html/index.html' (don't leave out the leading slash, and use slash not backslash).

Having opened that InputStream (perhaps wrapping it in a Reader) you can copy from there to the OutputStream of the HttpServletResponse (or a Writer wrapped around this OutputStream).

The reason you might want to open as a Reader and write as Writer is if the encoding of the file (in the jar) is not the encoding you want the HTML sent out as (e.g. as set in the response header).

(This is a generic approach, of course, and has nothing to do with REST or AngularJS.)



来源:https://stackoverflow.com/questions/22333647/refer-to-a-web-page-inside-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!