Jungleford's Home BlogJava分舵

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

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

          jungleford如是說(shuō)

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

          getResource和getResourceAsStream

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

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


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

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

              以下是在我寫(xiě)的一個(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中讀文本資源也是類(lèi)似方法調(diào)用getTextFromJar。
              需要說(shuō)明的是,這段代碼也不是我原創(chuàng)的,是從一段別的代碼中經(jīng)過(guò)修改得到的,但原代碼的來(lái)源忘記了,在這里向原作者表示感謝和歉意。

          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)  編輯  收藏 所屬分類(lèi): 咖啡屋 - Java 技術(shù)研究

          Feedback

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

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

          主站蜘蛛池模板: 大新县| 高要市| 句容市| 芒康县| 茂名市| 定陶县| 昌黎县| 大冶市| 子长县| 襄城县| 永顺县| 扶余县| 龙游县| 射阳县| 龙口市| 余姚市| 大同市| 东丽区| 龙陵县| 理塘县| 濮阳县| 敖汉旗| 灯塔市| 都兰县| 桂平市| 江山市| 嘉义县| 博爱县| 罗城| 休宁县| 台南县| 南通市| 礼泉县| 夏津县| 乳源| 兴安盟| 浦城县| 西华县| 衡南县| 石阡县| 和政县|