Detailed explanation of Java reading resource file and sample code in Jar

This article mainly introduces the details of Java reading resource files and implementation code in jar. When developing Java projects, jar packages are often used. Here we talk about how to read them. You can refer to the following for your friends
Detailed explanation of Java reading resource file and implementation code in Jar

       Directly on the code, the annotation part of the article is clear. You can refer to the following,

Tool class source code: ResourceLoadFromJarUtil.java

Implementation code:

import java.io.IOException; 
import java.io.InputStream; 
import java.net.JarURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.jar.JarEntry; 
import java.util.jar.JarFile; 
  
import org.apache.commons.io.IOUtils; 
  
public class ResourceLoadFromJarUtil { 
  
 /** 
  * @param args 
  * @throws IOException 
  */
 public static void main(String[] args) throws IOException { 
  
  /* Resource file path, cannot start with '/' character */ 
  String resourcePath = "struts-plugin.xml"; 
  
  /* Get all jar paths under ClassPath */ 
  String[] cps = System.getProperty("java.class.path").split(";"); 
  
  /* Read local Jar file */ 
  for (String cp : cps) { 
   if (!cp.endsWith(".jar")) { 
    continue; 
   } 
   InputStream in = loadResourceFromJarFile(cp, resourcePath); 
   if (in != null) { 
    System.err.println(IOUtils.toString(in)); 
    in.close(); 
   } 
  } 
  
  /* Read local Jar file */ 
  for (String cp : cps) { 
   if (!cp.endsWith(".jar")) { 
    continue; 
   } 
   InputStream in = loadResourceFromJarURL(cp, resourcePath); 
   if (in != null) { 
    System.err.println(IOUtils.toString(in)); 
    in.close(); 
   } 
  } 
  
  /* Read network Jar file */ 
  InputStream in = loadResourceFromJarURL( 
    "http://localhost:8080/SpringStruts2Integration/struts2-spring-plugin-2.3.4.1.jar", resourcePath); 
  if (in != null) { 
   System.err.println(IOUtils.toString(in)); 
   in.close(); 
  } 
 } 
  
 /** 
  * Read resources in Jar file 
  * 
  * @param jarPath 
  *   Local jar file path 
  * @param resPath 
  *   Path in the jar where the resource file is located (cannot start with '/' character) 
  * @return If the resource fails to load, return null 
  */ 
 public static InputStream loadResourceFromJarFile(String jarPath, String resPath) { 
  if (!jarPath.endsWith(".jar")) { 
   return null; 
  } 
  try { 
   JarFile jarFile = new JarFile(jarPath); 
   JarEntry jarEntry = jarFile.getJarEntry(resPath); 
   if (jarEntry == null) { 
    return null; 
   } 
   return jarFile.getInputStream(jarEntry); 
  } catch (IOException e) { 
   e.printStackTrace(); 
   return null; 
  } 
 } 
  
 /** 
  * Read resources in Jar file 
  * 
  * @param jarUrl 
  *   Local jar file or jar file path on the network (TTP: / / host: port / subpath / jarfile. Jar) 
  * @param resPath 
  *   Path in the jar where the resource file is located (cannot start with '/' character) 
  * @return If the resource fails to load, return null 
  */
 public static InputStream loadResourceFromJarURL(String jarUrl, String resPath) { 
  if (!jarUrl.endsWith(".jar")) { 
   return null; 
  } 
  URL url = null; 
  if (jarUrl.startsWith("http://")) { 
   try { 
    url = new URL("jar:" + jarUrl + "!/"); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    return null; 
   } 
  } else { 
   try { 
    url = new URL("jar:file:/" + jarUrl + "!/"); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    return null; 
   } 
  } 
  try { 
   JarURLConnection jarURLConnection; 
   jarURLConnection = (JarURLConnection) url.openConnection(); 
   JarFile jarFile = jarURLConnection.getJarFile(); 
   JarEntry jarEntry = jarFile.getJarEntry(resPath); 
   if (jarEntry == null) { 
    return null; 
   } 
   return jarFile.getInputStream(jarEntry); 
  } catch (IOException e) { 
   e.printStackTrace(); 
   return null; 
  } 
 } 
} 

The above is an example of java reading jar resources

Tags: Java network Apache Struts

Posted on Fri, 08 Nov 2019 11:00:55 -0500 by KrisCons