0

我有一个具有平面文件(或说文本文件)的动态 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 作为应用服务器。

我在动态 Web 项目的 WebContent 目录中添加了资源文件。作为一种不同的方法,我尝试了这个:

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 中或者没有部署它???有人可以帮我吗?

4

2 回答 2

2

首先,检查 Nationwide.war 以确保包含 rateJSON.txt。如果是,请尝试:

String rootPath = getServletConfig().getServletContext().getRealPath("/");
File file = new File(realPath + "WEB-INF/rateJSON.txt");
InputStream is = new FileInputStream(file);
于 2009-10-17T03:55:14.897 回答
1

我认为这个(getServletContext().getRealPath())在文档中......

如果 servlet 容器由于任何原因(例如当内容从 .war 存档中可用时)无法将虚拟路径转换为真实路径,则此方法返回 null。

于 2011-03-24T16:27:16.933 回答