读取jar包中的图片的代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

如下代码,使用getResourceAsStream返回jar中文件的InputStream流:

Image img = null;

try {
  MediaTracker m = new MediaTracker(this);
  InputStream is = getClass().getResourceAsStream("image.gif");
  //
  // if your image is in a subdir in the jar then
  //    InputStream is = getClass().getResourceAsStream("img/image.gif");
  //  for example
  //
  BufferedInputStream bis = new BufferedInputStream(is);
  // a buffer large enough for our image
  //
  // can be
  //   byte[] byBuf = = new byte[is.available()];
  //   is.read(byBuf);  or something like that...
  byte[] byBuf = = new byte[10000]; 

  int byteRead = bis.read(byBuf,0,10000);
  img = Toolkit.getDefaultToolkit().createImage(byBuf);
  m.addImage(img, 0);
  m.waitForAll();
  }
 }
catch(Exception e) {
  e.printStackTrace();
}