java獲取資源文件

yefx 9年前發布 | 2K 次閱讀 Java

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.geom.GeneralPath;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.ImageIcon;

/**

  • 功能:讀取資源文件類 <br>
  • 時間:2010-11-24 <br>
  • 備注: <br>
  • @author Lin.~
  • */ public abstract class ResourceUtil {

    /**

    • 得到資源文件
    • @param path
    • 資源文件的路徑
    • @return */ protected final URL getURL(String path) { // 先從當前目錄取(打成jar包的情況下) URL url = getClass().getResource(path); // 如果沒取到,則從根目錄取(打成jar包的情況下) if (url == null)

       url = getClass().getResource("/" + path);
      

      // 從當前線程的地址取 if (null == url)

       url = Thread.currentThread().getContextClassLoader()
               .getResource(path);
      

      // 以上代碼都是針對swing的。下面代碼針對eclipse中情況 if (url == null) {

       try {
           String rootPath = System.getProperty("user.dir");
           // 針對在eclipse中,用Java Application運行的。
           File webFile = new File(rootPath + "/" + path);
           if (webFile.exists()) {
               url = webFile.toURI().toURL();
           } else {
               // 針對eclipse中用web運行的。
               webFile = new File(Thread.currentThread().getContextClassLoader()
                       .getResource("/")+"/"+path);
               url = webFile.toURI().toURL();
           }
           // 實在不行了,死馬當活馬醫吧
           if(null ==url)
               url = new File(new File("").getAbsoluteFile()+"/"+path).toURI().toURL();
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }
      

      } if(null == url)

       throw new NullPointerException("對不起,始終沒有找到【"+path+"】資源");
      

      return url; }

      /**

    • 得到資源文件讀取流
    • @param filePath
    • 資源文件路徑
    • @return 資源文件流
    • @throws IOException */ private InputStream getJarIO(String filePath) throws IOException { String JarPath = URLDecoder.decode(getClass().getProtectionDomain()

           .getCodeSource().getLocation().getFile(), "UTF-8");
      

      if (JarPath.startsWith("/"))

       JarPath = JarPath.substring(1);
      

      JarFile cJar = new JarFile(JarPath); JarEntry util = cJar.getJarEntry(filePath); return cJar.getInputStream(util); }

      /**

    • 修改資源文件
    • @param filePath
    • 資源文件路徑
    • @return 資源文件寫入流
    • @throws Exception */ protected final OutputStream getJarOut(String filePath) throws IOException { throw new IOException("沒有實現修改Jar包內部的文件的方法"); }

      /**

    • 讀取資源文件流
    • @param resourceName
    • @return */ protected final InputStream getIO(String filePath) { URL url = getURL(filePath); try {

       return url != null ? url.openStream() : getJarIO(filePath);
      

      } catch (IOException e) {

       e.printStackTrace();
      

      } return null; }

      /**

    • 得到圖片
    • @param path
    • 圖片路江
    • @return */ public ImageIcon getImageIcon(String path) { return new ImageIcon(getURL(path)); }

      /**

    • 縮放圖片
    • @param icon
    • 要縮放的圖片
    • @param width
    • 縮放寬度
    • @param height
    • 縮放高度
    • @return 縮放后的圖片 */ public static ImageIcon zoomImg(ImageIcon icon, int width, int height) { Image img = icon.getImage(); Image newImg = img.getScaledInstance(width, height, 1); return new ImageIcon(newImg); }

      /**

    • 繪制圓角圖片
    • @param icon
    • 要繪制的原圖
    • @param width
    • 繪制圓角寬
    • @param height
    • 繪制圓角高
    • @return 繪制完成的圖片 */ public static BufferedImage getCircularImage(ImageIcon icon, int width,int height) { BufferedImage buff = new BufferedImage(icon.getIconWidth(),

           icon.getIconHeight(), Image.SCALE_DEFAULT);
      
      

      Image i = icon.getImage(); Graphics2D g = (Graphics2D) buff.getGraphics(); buff = g.getDeviceConfiguration().createCompatibleImage(

           icon.getIconWidth(), icon.getIconHeight(),
           Transparency.TRANSLUCENT);
      

      g.dispose(); g = buff.createGraphics(); System.out.println(g.getColor()); g.setColor(Color.RED); g.setStroke(new BasicStroke(1)); RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0,

           icon.getIconWidth(), icon.getIconHeight(), width, height);
      

      GeneralPath p = new GeneralPath(); p.append(rect, false); g.setClip(p); g.drawImage(i, 0, 0, null); return buff; }

}</pre>

 本文由用戶 yefx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!