so erhalten Sie den relativen Pfad einer Ressource in einem j2ee-Projekt

Ich habe ein Dynamic Web Project mit einem flachen Datei (oder sagen text-Datei). Ich habe ein servlet erstellt, in die ich brauche, um diese Datei verwenden.

Mein code ist wie folgt:

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();
                 }
          }

}

Das problem ist, dass die InputStream ist hat null Wert.

Ich bin mir nicht sicher, wie man den richtigen relativen Pfad.
Ich bin mit JBOSS als app-server.

Habe ich die Ressource-Datei in das WebContent-Verzeichnis eines Dynamischen Web-Projekt.
Als einen anderen Ansatz weiter, ich versuchte dies:

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();
                 }
          }
  }

Den Wert der contextPath ist jetzt: C:\JBOSS\jboss-5.0.1.GA\server\default\tmp\4p72206b-uo5r7k-g0vn9pof-1-g0vsh0o9-b7\Nationwide.war\WEB-INF\rateJSON

Aber an dieser Stelle das rateJSON Datei ist nicht da? Es scheint JBOSS ist nicht setzen Sie diese Datei in die App.Krieg oder nicht bereitstellen???
Könnte mir bitte jemand helfen?

InformationsquelleAutor Neeraj | 2009-10-17
Schreibe einen Kommentar