0

フラット ファイル (またはテキスト ファイル) を持つ動的 Web プロジェクトがあります。このファイルを使用する必要があるサーブレットを作成しました。

私のコードは次のとおりです。

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に null 値があることです。

正しい相対パスを取得する方法がわかりません。アプリサーバーとして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())はドキュメントにあると思います...

このメソッドは、サーブレット コンテナが何らかの理由で仮想パスを実際のパスに変換できない場合 (コンテンツが .war アーカイブから利用可能になっている場合など)、null を返します。

于 2011-03-24T16:27:16.933 に答える