[英]Java Crash while downloading File
我想從我自己的服務器上下載一個大的.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文件的讀取中。
我通過解決方法解決了此問題。
URL downloadURL = new URL(versionCode[1]);
HttpURLConnection urlConnection = (HttpURLConnection) 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();
Files.copy(inputStream, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.