Access file in WebContent folder from a servlet

前端 未结 3 746
生来不讨喜
生来不讨喜 2021-02-06 04:56

I\'m trying to generate a PDF document using FOP. The pdf generation code is kept in a servlet and the xsl is in a specific folder in the WebContent folder.

How can I a

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 05:47

    For a direct and independent container implementation, you can access the resourcewith the following method getResource() inside your servlet:

    /start servlet/

    public InputStream getResource(String resourcePath) {
      ServletContext servletContext = getServletContext();
      InputStream openStream = servletContext.getResourceAsStream( resourcePath );
      return openStream;
    }
    
    public void testConsume() {
      String path = "WEB-INF/teste.log";
      InputStream openStream = getResource( path );
    
      int c = -1;
      byte[] bb = new byte[1024];
      while ( -1 != ( c = openStream.read( bb ) ) ) {
        /* consume stream */
      }
      openStream.close();
    }
    

    /end servlet/

提交回复
热议问题