读取JAR包中的文件
读取JAR包中文件方法,有三种。
一、通过类加载器获取
public class App {
public static void main(String[] args) {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("logback.xml");
}
}
二、使用ClassPathResource获取
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import java.io.InputStream;
public class App {
public static void main(String[] args) {
ClassPathResource classPathResource = new ClassPathResource("logback.xml");
InputStream is = classPathResource.getStream();
FastByteArrayOutputStream read = IoUtil.read(is);
System.out.println(read);
}
}
三、使用ResourceUtil
也可以读取JAR包以外的文件
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil;
import java.io.InputStream;
public class App {
public static void main(String[] args) {
Resource resourceObj = ResourceUtil.getResourceObj("join_config.properties");
InputStream is = resourceObj.getStream();
FastByteArrayOutputStream read = IoUtil.read(is);
System.out.println(read);
}
}
四、打包时排除resource下的文件打包到jar中
使用MAVEN插件打包时,可以过滤掉resources目录下的文件,如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<excludes>
<!--排除jar包中classes目录下的配置文件-->
<exclude>*.*</exclude>
</excludes>
</configuration>
</plugin>
在web框架中的请求处理器中的使用示例
import com.lastb7.swagger.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author JHL
* @version 1.0
* @date 2022/8/8 17:29
* @since : JDK 11
*/
@Slf4j
public class Test {
public void handle(HttpServletRequest request, HttpServletResponse response) {
String requestURI = request.getRequestURI();
if (requestURI.contains("webjars/") || requestURI.contains("doc.html")) {
String path = "META-INF/resources" + requestURI;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
OutputStream outputStream = null;
try {
if (inputStream != null) {
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, response.getOutputStream());
}
} catch (IOException e) {
log.error("无法读取jar包中的静态资源", e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
}
}
maven目录的理解
使用maven管理的源代码中的java目录与resources目录编译前表示的是不同的目录,用于区分源代码与配置文件,但代码经过编译过后会将两个目录合为一个classes目录,并不区分二者。
java类路径的理解
D:\jdk11\jdk-11.0.9\bin\java.exe
-javaagent:D:\soft\IDEA2021\IDEA\lib\idea_rt.jar=52997:D:\soft\IDEA2021\IDEA\bin
-Dfile.encoding=UTF-8
# 在idea本地启动时,类的搜索路径在启动时会自动解析pom文件生成,打成服务部署包时要尤为注意,打包后的包中是否包含三方库,是外部挂载类搜索路径还是将三方库直接打到部署包中需根据实际情况选择
# classpath类路径加载的jar包与编译目录,在jvm中实际上是等价的
-classpath
D:\code\project\jfinaltest\target\classes;
D:\mvn-repository\commons-io\commons-io\2.11.0\commons-io-2.11.0.jar;
D:\mvn-repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;
D:\mvn-repository\cn\hutool\hutool-all\5.8.0.M4\hutool-all-5.8.0.M4.jar;
D:\mvn-repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;
D:\mvn-repository\com\google\guava\guava\30.1.1-jre\guava-30.1.1-jre.jar;
D:\mvn-repository\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;
D:\mvn-repository\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;
D:\mvn-repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;
D:\mvn-repository\org\checkerframework\checker-qual\3.8.0\checker-qual-3.8.0.jar;
D:\mvn-repository\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;
D:\mvn-repository\com\jfinal\jfinal\5.0.1\jfinal-5.0.1.jar;
D:\mvn-repository\ch\qos\logback\logback-access\1.2.3\logback-access-1.2.3.jar;
D:\mvn-repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;
D:\mvn-repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;
D:\mvn-repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;
D:\mvn-repository\com\lastB7\jfinal-swagger-knife4j\2.10.5\jfinal-swagger-knife4j-2.10.5.jar;
D:\mvn-repository\com\github\xiaoymin\knife4j-spring-ui\2.0.9\knife4j-spring-ui-2.0.9.jar;
D:\mvn-repository\io\swagger\swagger-annotations\1.6.2\swagger-annotations-1.6.2.jar;
D:\mvn-repository\org\apache\kafka\kafka-clients\2.8.0\kafka-clients-2.8.0.jar;
D:\mvn-repository\com\github\luben\zstd-jni\1.4.9-1\zstd-jni-1.4.9-1.jar;
D:\mvn-repository\org\lz4\lz4-java\1.7.1\lz4-java-1.7.1.jar;
D:\mvn-repository\org\xerial\snappy\snappy-java\1.1.8.1\snappy-java-1.1.8.1.jar
# 主启动类 # 启动参数
com.test.App