首页 > 代码库 > 获取网站资源 getResourceAsStream

获取网站资源 getResourceAsStream

 1     获取网站资源(重点)
 2 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 3         // 获取网站对象
 4         ServletContext context = this.getServletContext();
 5         // 获取网站中的静态资源
 6         InputStream in = context.getResourceAsStream("/123.bmp");
 7         // 获取输出流
 8         FileOutputStream out = new FileOutputStream(new File("d:/123.bmp"));
 9         // 边读 边写
10         byte[] bs  = new byte[1024];
11         int len = 0;
12         while((len = in.read(bs)) != -1){
13             out.write(bs, 0, len);
14             out.flush();
15         }
16         // 释放资源
17         in.close();
18         out.close();
19     }