servlet读取Context 文件

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String ip = request.getRemoteAddr();
		System.out.println(ip);
		
		//读取 3.txt
		String path3 = getServletContext().getRealPath("/WEB-INF/5.txt");
		
		System.out.println(path3);
		
		readContent(path3);
		
		
	}

	
	public static void readContent(String path){
		try {
			InputStream in = new FileInputStream(path);
			
			byte[] buf = new byte[1024];
			int len = 0 ;
			while((len = in.read(buf))!= -1){
				System.out.println(new String(buf,0,len));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 

原文链接: servlet读取Context 文件 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( http://gyarmy.com/post-52.html )

发表评论

0则评论给“servlet读取Context 文件”