[英]How do I access a text file from within my war
How do I know what file reference to use to get a file from my WAR. 我如何知道用于从WAR获取文件的文件引用。
The structure of the WAR is: WAR的结构是:
WAR
战争
srcSRC
- model- 模特
- web- 网络
build建立
WebContent网页内容
META-INFMETA-INF
WEB-INFWEB-INF
LIBLIB
The JSPs are under WebContent, I have put the config.txt file under the WebContent folder and tried to get to it with JSP在WebContent下,我已将config.txt文件放在WebContent文件夹下并尝试使用它
BufferedReader in = new BufferedReader(new FileReader("WebContent/config.txt"));
BufferedReader in = new BufferedReader(new FileReader(“WebContent / config.txt”));
But this doesn't work. 但这不起作用。 Does anyone know what reference I need to pass or how I can figure it out.
有谁知道我需要传递什么参考或我如何弄明白。
Check out ServletContext.getResourceAsStream() . 查看ServletContext.getResourceAsStream() 。 Here is an example .
这是一个例子 。
我想只是在读取文件时从路径中删除WebContent,它应该有所帮助。
One way to do this comes with a caveat that I'll explain below. 一种方法是提出一个警告,我将在下面解释。 You should do something like this:
你应该做这样的事情:
// In a Servlet
ServletContext sc = getServletContext();
BufferedReader in =
new BufferedReader(new FileReader(sc.getRealPath("WebContent/config.txt"));
This assumes that your servlet container is configured to expand war files into their corresponding directory structure. 这假设您的servlet容器配置为将war文件扩展到其相应的目录结构中。
The safest way to access this file should be something like this: 访问此文件最安全的方法应该是这样的:
InputStream input = getClass().getClassLoader().getResourceAsStream("your/path/enter/here"); InputStream input = getClass()。getClassLoader()。getResourceAsStream(“your / path / enter / here”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.