2012-09-17 2 views
0

WEB-INF 디렉토리에 웹 애플리케이션과 XML 파일이 있습니다. 이 xml 파일을로드해야합니다. 를 넣기보다는 내가 뭔가를 작동하지 않습니다 Thread.currentThread().getContextClassLoader().getResourceAsStream("/WEB-INF/test.xml");WEB-INF 폴더 아래에 리소스로드 중

같은 옵션을 시도 getServletContext().getResourceAsStream("/WEB-INF/test.xml");

를 사용하는 다른 방법이있다.

도와 주시겠습니까?

+0

Q : 정확히 "getServletContext()"메소드에 문제가 있습니까? – paulsm4

+0

정적 블록에서로드하고 싶습니다. – Don

+0

duplicate [웹 아카이브의 WEB-INF 디렉토리에서 리소스를로드하는 방법] (http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a- 웹 아카이브) – user1406062

답변

1

WEB-INF 폴더의 리소스에 액세스하려면 응용 프로그램 (jsp - servlet) 내에서 /WEB-INF/test.xml을 사용하면됩니다.

<!%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles" %> 

웹 사용자가 사용할 수 있도록하려면 직접 액세스 할 수 없습니다. 사용자가 직접 액세스하려면 WEB-INF 폴더 (예 : /WEB-INF/test.xml 파일을 읽고 jsp/servlet에 출력하는 jsp)에서 파일을 노출하는 일종의 인터페이스가 있어야합니다)

UPDATE

는 서블릿의 사용이를 사용하여 파일을 읽으려면 :

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    StringBuffer strContent = new StringBuffer(""); 
    int ch; 
    PrintWriter out = response.getWriter(); 
    try { 
     String path = getServletContext().getRealPath("/WEB-INF/newfile.xml"); 
     File file = new File(path); 
     FileInputStream fin = null; 
     try { 
      fin = new FileInputStream(file); 
      while ((ch = fin.read()) != -1) { 
       strContent.append((char) ch); 
      } 
      fin.close(); 
     } catch (FileNotFoundException e) { 
      System.out.println("File " + file.getAbsolutePath() 
        + " could not found"); 
     } catch (IOException ioe) { 
      System.out.println("Exception while reading the file" + ioe); 
     } 
     System.out.println("File contents :"); 
     System.out.println(strContent); 
    } finally { 
     out.close(); 
    } 
} 

WEB-INF에서 newfile.xml라는 이름의 파일을 읽고 콘솔에 출력 할 내용됩니다.

파일 내용 : 출력은 같을 것이다

< A>

< B> XXXX </B>

</A>

+0

내 서블릿 클래스의 정적 블록에서 이것을 읽어야합니다. – Don

+0

내 업데이트 된 답변보기 – MaVRoSCy

+0

내 수업 중 하나의 정적 블록에서이 작업을 수행해야합니다. 거기에서 getServletContext()를 호출 할 수 없습니다. – Don

관련 문제