java - 下载文件时 Java 崩溃
问题描述
我想从我自己的服务器上下载一个大的 .jar 文件。( https://luis.team ) 在那里我做了一个线程来下载它,因为它非常巨大。这是我的下载代码:
public Update(String version, File outputFile) {
URL url;
URLConnection connection = null;
try {
url = new URL("https://raw.githubusercontent.com/Luuuuuis/InstantVerify/master/version");
connection = url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
assert connection != null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String[] versionCode = in.readLine().split("&&");
if (!(versionCode[0].equalsIgnoreCase(version))) {
/*
* Download from URL given in version file
*/
if(versionCode[1] != null && !versionCode[1].equalsIgnoreCase("null")) {
Thread th = new Thread(() -> {
try {
URL downloadURL = new URL(versionCode[1]);
URLConnection urlConnection = downloadURL.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
InputStream inputStream = urlConnection.getInputStream();
OutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
InstantVerify.version += " (outdated)";
} catch (Exception ex) {
ex.printStackTrace();
}
});
th.start();
}
} else {
InstantVerify.version += " (latest)";
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
在此之后,我得到一个非常小的文件(大约 30MB)和一个 Java 运行时错误:
A fatal error has been detected by the Java Runtime Environment:
SIGBUS (0x7) at pc=0x00007f8fd9b5ed10, pid=14021, tid=0x00007f8fa5b56700
JRE version: Java(TM) SE Runtime Environment (8.0_201-b09) (build 1.8.0_201-
b09)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode linux-
amd64 compressed oops)
Problematic frame:
C [libzip.so+0x11d10] newEntry.isra.4+0x60
Failed to write core dump. Core dumps have been disabled. To enable core
dumping, try "ulimit -c unlimited" before starting Java again
An error report file with more information is saved as:
/home/ServerTest/Bungee/hs_err_pid14021.log
Compiled method (nm) 6767 201 n 0
java.util.zip.ZipFile::getEntry (native)
total in heap [0x00007f8fc517a510,0x00007f8fc517a868] = 856
relocation [0x00007f8fc517a638,0x00007f8fc517a678] = 64
main code [0x00007f8fc517a680,0x00007f8fc517a868] = 488
If you would like to submit a bug report, please visit:
http://bugreport.java.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.
Aborted
我想 Java 中的内存映射存在问题,因为我的根服务器只有 2GB 的 RAM。我能做些什么来解决这个问题?谢谢你。
解决方案
SIGBUS 错误是操作系统抛出的未对齐数据错误。这基本上意味着数据未针对您的服务器运行的平台正确对齐。例如,一些 Unix 系统坚持所有数据都在 8 字节边界上对齐。看起来文件已成功下载,并且是对 java.util.zip.ZipFile::getEntry 的调用导致崩溃。如果您在该平台上启用核心转储,Java 开发人员应该能够确定错误是在初始 zip 文件的构建中还是在 zip 文件的读取中。
推荐阅读
- json - 如何使用appscript将谷歌表格的唯一数据获取到json?
- java - 使用 Java 在 TestNG 中传递多个可选默认值
- ios - 如何从后台线程执行 CoreData context.count?
- java - 如何使 springboot 应用程序在 -Xmx450m 上运行这个 jvm 参数抛出 OutOfMemoryError ,目前它没有在 heroku 中低于 -Xmx1000m
- java - 如何在 Java 8 Streams 中编写多个过滤器和映射
- python - 用另一个 csv 文件中的更新信息更新现有 csv 文件
- node.js - 在 GitHub Actions 中启动 nuxt 时出现 Nuxt 致命错误
- list - List.from 和 List.add 有什么区别
- flutter - 注销并重新登录后,仍然显示旧用户数据
- sql-server-2019-express - SQL Server 2019 Express 安装失败,因为它无法安装 Microsoft ODBC Driver for SQL Server