有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Resources for html和rest server

我正在尝试使用JavaREST服务器创建一个网站。为了能够加载预制html文件,我需要知道它们在我的服务器上保存的位置。我可以在谷歌或其他任何地方找到关于这个话题的任何信息,所以要么我是瞎子,要么答案太容易被提及:D

所以是的,我正在部署我的。war文件在外部jersey rest服务器上,我希望能够加载附加到该服务器的文件。war文件(包含在eclipse项目的WEB-INF文件夹中)。我怎么才能拿到这些

再次提前感谢:)


共 (1) 个答案

  1. # 1 楼答案

    如果资源是JSP,则可以执行以下操作:

    @GET
    public void getHome(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            ...
            request.getRequestDispatcher("/WEB-INF/home.jsp").forward(request, response);
        } catch (ServletException | IOException e) {
            LOG.log(Level.SEVERE, "There was an error getting the ...", e);
        }
    }
    

    如果资源是图像:

    @GET
    @Path("/get/{name}")
    @Produces("image/png")
    public Response getFile(@Context ServletContext context, @PathParam("name") String name) {
    
        String fullPath = context.getRealPath("/WEB-INF/images/" + name);
        File file = new File(fullPath);
    
        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition", "attachment; filename=" + name);
        return response.build();
    
    }
    

    等等