servlet读取Context 文件

2016-7-7 流沙 Android

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 读取资源文件

发表评论:

Powered by 流沙团

备案号:鄂ICP备15017378号-1