从Servlet访问WebContent文件夹中的文件 [英] Access file in WebContent folder from a servlet
问题描述
我正在尝试使用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
的类.如果this.getServletContext()
是您的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屋!