加载器关系
当使用 tomcat 容器时,运行时上下文类加载器是 TomcatEmbeddedWebappClassLoader,其父类加载器是 LaunchedURLClassLoader。在IDEA中运行时,上下文类加载器是 AppClassLoader,即应用程序类加载器。
TomcatServletWebServerFactory设置TomcatEmbeddedWebappClassLoader类加载的时候,会设置parentclassloader即LaunchedURLClassLoader,因为项目是通过LaunchedURLClassLoader加载器加载的,如下代码可测
@SpringBootApplication
public class SpringbootApplication {
/**
* java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
*
* @param args
*/
public static void main(String[] args) {
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println("SpringbootApplication类装载器:" + currentClassLoader);
ClassLoader oneParentClassLoader = currentClassLoader.getParent();
System.out.println("SpringbootApplication的父类加载器——扩展类加载器:" + oneParentClassLoader);
ClassLoader twoClassLoader = oneParentClassLoader.getParent();
System.out.println("SpringbootApplication的父类的父类加载器——引导类加载器:" + twoClassLoader);
SpringApplication.run(SpringbootApplication.class, args);
// SpringbootApplication类装载器:org.springframework.boot.loader.LaunchedURLClassLoader@238e0d81
// SpringbootApplication的父类加载器——扩展类加载器:sun.misc.Launcher$AppClassLoader@55f96302
// SpringbootApplication的父类的父类加载器——引导类加载器:sun.misc.Launcher$ExtClassLoader@3d646c37
}
}
问题注意点
jar启动时候:读取resources下的xlsx文件时候,fis.available()=0,txt文本可以正常读取
idea启动时候:任何文件都可以
结论:jar启动读取xlsx时候使用LaunchedURLClassLoader的RandomAccessDataFile$DataInputStream进行流包装,没有重写available方法,即available=0,这里是JarFileEntries方法,普通文件使用ZipInflaterInputStream进行包装,像xlsx就不会
getMethod说明:所述getMethod()函数是java.util.zip包的一部分。该函数返回作为参数传递的特定ZipEntry的压缩方法;如果未指定,则返回-1。这里我是真没明白,到底啥意思
参考:https://www.geeksforgeeks.org/java-zipentry-getmethod-function-with-examples/
InputStream getInputStream(FileHeader entry) throws IOException {
if (entry == null) {
return null;
} else {
InputStream inputStream = this.getEntryData(entry).getInputStream();
if (entry.getMethod() == 8) {
inputStream = new ZipInflaterInputStream((InputStream)inputStream, (int)entry.getSize());
}
return (InputStream)inputStream;
}
}
栗子
Thread thread = Thread.currentThread();
ClassLoader contextClassLoader = thread.getContextClassLoader();
if (contextClassLoader != null) {
fis = contextClassLoader.getResourceAsStream(filePath);
}
// int length = fis.available();// 这个判断不准,直接用下边read即可
// while (length > 0) {
// int hasRead = fis.read(bytes, 0, Math.min(length, BUFFER_LENGTH));
// response.getOutputStream().write(bytes, 0, hasRead);
// length -= hasRead;
// }
int bytesRead;
while ((bytesRead = fis.read(bytes)) != -1) {
response.getOutputStream().write(bytes, 0, bytesRead);
}