从 servlet 访问 WebContent 文件夹中的文件

发布于 2024-08-06 09:35:53 字数 205 浏览 2 评论 0原文

我正在尝试使用 FOP 生成 PDF 文档。 pdf 生成代码保存在 servlet 中,xsl 位于 WebContent 文件夹中的特定文件夹中。

如何通过给出相对路径来访问此 xsl 文件?仅当我在文件对象中给出完整路径时它才有效。

我需要动态生成 xml 内容。如何将这个动态生成的 xml 作为源而不是 File 对象?

请提供您的建议。

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 access this xsl file by giving a relative path? It works only if I give the complete path in the File object.

I need to generate the xml content dynamically. How can I give this dynamically generated xml as the source instead of a File object?

Please provide your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞琼窗梦回ˉ 2024-08-13 09:35:54

要获取路径,您可以执行以下操作:

String path = s.getServletContext().getRealPath("/WEB-INF/somedir/hdfeeh");         

s 是实现 HTTPServlet 的类。如果您的需要,您也可以使用 this.getServletContext() Servlet 类。

然后将其作为参数传递。

至于使用动态生成的 XML,您使用的库应该支持使用输入流,编写 XML,将其转换为字节数组,然后将其包装在 ByteArrayInputStream 中并使用它。

To get the path you can just do:

String path = s.getServletContext().getRealPath("/WEB-INF/somedir/hdfeeh");         

s is the class that implements HTTPServlet.You can also use this.getServletContext() if its your servlet class.

Then pass this as a parameter.

As far as using dynamically generated XML, the library you're using should support using an input stream, write your XML, convert it to a byte array, then wrap it in a ByteArrayInputStream and use this.

挽你眉间 2024-08-13 09:35:54

对于直接且独立的容器实现,您可以在 servlet 中使用以下方法 getResource() 访问资源:

/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/

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/

朕就是辣么酷 2024-08-13 09:35:54

我使用以下方法读取网页内容下的文件

BufferedReader reader = new BufferedReader(new InputStreamReader(request.getSession().getServletContext().getResourceAsStream("/json/sampleJson.json")));

现在所有文件内容都在阅读器对象中可用。

I used the following method to read the file under web content

BufferedReader reader = new BufferedReader(new InputStreamReader(request.getSession().getServletContext().getResourceAsStream("/json/sampleJson.json")));

Now all the file content is available in the reader object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文