从 servlet 访问 WebContent 文件夹中的文件 [英] Access file in WebContent folder from a servlet

查看:29
本文介绍了从 servlet 访问 WebContent 文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如何通过提供相对路径来访问此 xsl 文件?仅当我在 File 对象中提供完整路径时才有效.

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.

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

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

请提供您的建议.

推荐答案

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

To get the path you can just do:

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

s 是实现 HTTPServlet 的类.如果它是你的 servlet 类,你也可以使用 this.getServletContext().

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

然后将其作为参数传递.

Then pass this as a parameter.

就使用动态生成的 XML 而言,您使用的库应该支持使用输入流,编写您的 XML,将其转换为字节数组,然后将其包装在 ByteArrayInputStream 中并使用这个.

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.

这篇关于从 servlet 访问 WebContent 文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆