我有一个动态Web项目有一个平面文件(或说文本文件)。我创建了一个servlet,我需要在其中使用这个文件。
我的代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String resource = request.getParameter ("json") ;
if ( resource != null && !resource.equals ( "" ) ) {
//use getResourceAsStream ( ) to properly get the file.
InputStream is = getServletContext ().getResourceAsStream ("rateJSON") ;
if ( is != null ) { // the resource exists
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( ) ;
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
out.flush();
}
}
}
问题是 InputStream 具有空值。
我不确定如何获得正确的相对路径。 我正在使用JBOSS作为应用服务器。
我已将资源文件添加到Dynamic Web Project的WebContent目录中。 作为一个不同的approch,我尝试了这个:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletConfig config = getServletConfig();
String contextName = config.getInitParameter("ApplicationName");
System.out.println("context name"+ contextName);
String contextPath = config.getServletContext().getRealPath(contextName);
System.out.println("context Path"+contextPath);
//contextPath = contextPath.substring(0, contextPath.indexOf(contextName));
contextPath += "\\rateJSON.txt";
System.out.println(contextPath);
String resource = request.getParameter ("json") ;
System.out.println("Hi there1"+resource);
if ( resource != null && !resource.equals ( "" ) ) {
System.out.println("Hi there");
//use getResourceAsStream ( ) to properly get the file.
//InputStream is = getServletContext ().getResourceAsStream (resource) ;
InputStream is = getServletConfig().getServletContext().getResourceAsStream(contextPath);
if ( is != null ) { // the resource exists
System.out.println("Hi there2");
response.setContentType("application/json");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
StringWriter sw = new StringWriter ( );
for ( int c = is.read ( ) ; c != -1; c = is.read ( ) ) {
sw.write ( c ) ;
System.out.println(c);
}
PrintWriter out = response.getWriter();
out.print (sw.toString ()) ;
System.out.println(sw.toString());
out.flush();
}
}
}
contextPath的值现在是:C:\ JBOSS \ jboss-5.0.1.GA \ server \ default \ tmp \ 4p72206b-uo5r7k-g0vn9pof-1-g0vsh0o9-b7 \ Nationwide.war \ WEB-INF \ rateJSON
但是在这个位置,rateJSON文件不存在?似乎JBOSS没有把这个文件放在App.war中或者没有部署它? 有人可以帮助我吗?
答案 0 :(得分:2)
首先,检查Nationwide.war以确保包含rateJSON.txt。如果是,请尝试:
String rootPath = getServletConfig().getServletContext().getRealPath("/");
File file = new File(realPath + "WEB-INF/rateJSON.txt");
InputStream is = new FileInputStream(file);
答案 1 :(得分:1)
我认为这个( getServletContext()。getRealPath())在docs ...
如果servlet容器由于任何原因(例如,当从.war存档中提供内容时)无法将虚拟路径转换为实际路径,则此方法返回null。