Jungleford's Home BlogJava分舵

          Java技術(shù)研究,兼探討歷史話題

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            24 Posts :: 0 Stories :: 53 Comments :: 0 Trackbacks

          jungleford如是說

              可能有不少初學(xué)者會(huì)有這樣的困惑(以前我也有過):在你的代碼里調(diào)用了一些資源文件,如圖片,音樂等,在調(diào)試環(huán)境或單獨(dú)運(yùn)行的時(shí)候可以正常顯示或播放,而一旦打包到j(luò)ar文件中,這些東東就再也出不來了,除非把這個(gè)jar放到原來未打包以前的目錄下,但通常jar是單獨(dú)發(fā)布的。這里介紹一個(gè)解決這類問題的方法。

          getResource和getResourceAsStream

              問題的根源還是在于老生常談的所謂class path,不信的話你在系統(tǒng)環(huán)境變量里的ClassPath加上你的jar文件,這下你就看得到你的圖片了!但單獨(dú)發(fā)布jar的話不可能指望每次都讓用戶為你的jar而專門修改classpath。那么有沒有什么辦法一勞永逸地搞定它呢?我們需要從類的裝載入手。先扯遠(yuǎn)一點(diǎn),在開發(fā)JSP之類的Web應(yīng)用程序的時(shí)候要用到第三方的庫怎么辦?通常的做法是把這些庫(可以是class,也可以是jar)統(tǒng)統(tǒng)放到WEB-INF/lib/目錄下面,為什么這樣系統(tǒng)就認(rèn)了呢?因?yàn)閃eb容器(譬如Tomcat)在裝載類的時(shí)候有自己的組織方式(可以參考Tomcat手冊(cè))。特別地,jar也是類裝載器的一個(gè)可訪問媒介,ClassLoader提供了兩個(gè)方法用于從裝載的類路徑中取得資源:

          public URL getResource(String name);
          public InputStream getResourceAsStream(String name);


          這里name是資源的類路徑,它是相對(duì)與“/”根路徑下的位置。getResource得到的是一個(gè)URL對(duì)象來定位資源,而getResourceAsStream取得該資源輸入流的引用保證程序可以從正確的位置抽取數(shù)據(jù)。
              真正使用的不是ClassLoader的這兩個(gè)方法,而是Class的getResource和getResourceAsStream方法,因?yàn)镃lass對(duì)象可以從你的類得到(如YourClass.class或YourClass.getClass()),而ClassLoader則需要再調(diào)用一次YourClass.getClassLoader()方法,但根據(jù)JDK文檔的說法,Class對(duì)象的這兩個(gè)方法其實(shí)是“委托”(delegate)給裝載它的ClassLoader來做的,所以只需要使用Class對(duì)象的這兩個(gè)方法就可以了。
              在參考資料中有一篇老外寫的文章比較深入介紹了從jar中裝載資源的方法。

          一個(gè)應(yīng)用的例子

              以下是在我寫的一個(gè)小工具M(jìn)SNHistoryCombiner中用到的一段代碼,可以從jar中裝載圖片和文本信息。譬如,你的jar中根目錄下有個(gè)img目錄,里面放有一些圖片,如img1.jpg,你可以這樣調(diào)用

          Utilities.getImageFromJar("/img/img1.jpg", YourClass.class);


          注意必須這里是“/img/img1.jpg”而非“img/img1.jpg”。從jar中讀文本資源也是類似方法調(diào)用getTextFromJar。
              需要說明的是,這段代碼也不是我原創(chuàng)的,是從一段別的代碼中經(jīng)過修改得到的,但原代碼的來源忘記了,在這里向原作者表示感謝和歉意。

          import java.io.*;
          import java.awt.
          *;

          public class Utilities
          {
            
          /**
             * <p>
             * Description: Return an Image based on the supplied image identifier. The
             * image is assumed to reside at the defined location within the same
             * repository as this class.
             
          */

            
          public static Image getImageFromJar(final String imageId, Class c)
            
          {
              
          // Image reference initialised to null (the image may not be found).
              Image image = null;
              
          // Open a resource stream on the supplied image identifier.
              final InputStream inputStream = c.getResourceAsStream(imageId);
              
          // If the image data is found
              if (inputStream != null)
              
          {
                
          // Open a byte array output stream so that we can create a byte
                
          // array with which to create the image.
                final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                
          // Attempt to copy the source image data into the byte array
                
          // stream, and then create an image from the result.
                try
                
          {
                  
          // Read/write image data in 1k chunks.
                  final byte[] bytes = new byte[1024];
                  
          int read = 0;
                  
          while ((read = inputStream.read(bytes)) >= 0)
                  
          {
                    byteArrayOutputStream.write(bytes, 
          0, read);
                  }


                  
          // Create an image from the resulting byte array.
                  image = Toolkit.getDefaultToolkit().createImage(
                      byteArrayOutputStream.toByteArray());
                }

                
          catch (IOException exception)
                
          {
                  exception.printStackTrace();
                }

              }

              
          return image;
            }


            
          public static String getTextFromJar(final String filename, Class c)
            
          {
              String text 
          = "";
              
          // Open a resource stream on the supplied file name.
              final InputStream inputStream = c.getResourceAsStream(filename);
              
          // If the file is found
              if (inputStream != null)
              
          {
                final BufferedReader 
          in = new BufferedReader(new InputStreamReader(
                    inputStream));
                
          try
                
          {
                  String s;
                  
          while ((s = in.readLine()) != null)
                  
          {
                    text 
          += s + "\n";
                  }

                }

                
          catch (IOException exception)
                
          {
                  exception.printStackTrace();
                }

              }

              
          return text;
            }

          }


          參考資料

          J2SE API Documentation

          用 One-JAR 簡(jiǎn)化應(yīng)用程序交付

          posted on 2005-06-11 12:41 jungleford 閱讀(2985) 評(píng)論(2)  編輯  收藏 所屬分類: 咖啡屋 - Java 技術(shù)研究

          Feedback

          # re: 從Jar包獲取資源的方法 2006-02-28 09:52 skyswan
          如果不知道具體的資源文件名,想列出所有的資源,怎么做呢?  回復(fù)  更多評(píng)論
            

          # re: 從Jar包獲取資源的方法 2006-03-11 19:09 jungleford
          @skyswan
          我沒有具體作過,但我想作為jar包,java.util.jar中的工具應(yīng)該可以找到所有資源吧  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 遵义县| 从江县| 藁城市| 定远县| 思南县| 都匀市| 巨野县| 新晃| 勃利县| 涡阳县| 烟台市| 仁怀市| 秭归县| 黄冈市| 永福县| 柘城县| 兴安县| 虞城县| 长岛县| 太仆寺旗| 平罗县| 新田县| 庆元县| 满洲里市| 本溪| 平南县| 神池县| 资源县| 玛纳斯县| 洛川县| 信丰县| 调兵山市| 梧州市| 托里县| 巴林左旗| 灌云县| 万盛区| 仁布县| 嵩明县| 南木林县| 皮山县|