下载文件时 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)";


我想 Java 中的内存映射存在问题,因为我的根服务器只有 2GB 的 RAM。我能做些什么来解决这个问题?谢谢你。


扬帆大鱼
浏览 259回答 2
2回答

阿晨1998

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();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java