java.io.IOException:文件大小不正确(仅在linux(CentOS 6)上出现错误)



我正在尝试为游戏重建一个旧的java包装脚本,但我不知道为什么当我在VPS(CentOS 6)中运行它时会出现错误,我只有在那里运行它时才会出现错误,在我的mac或带有windows 10的pc中它不会发生。。。

在stackoverflow上已经看了一眼,我看到了一个常见的类似错误,那是因为大小变成了-1,但我想这里不是这样:

INFO: Downloading the latest version of StarMade (length: 179840020 bytes, URL: http://files.star-made.org/build/starmade-build_20160413_201539.zip)...

"ls-lha:"

-rw-r--r-- 1 root root 172M Apr 18 11:05 StarMade-latest.zip

和179840020字节~=171,5兆字节

错误如下:

java.io.IOException: File downloaded is the incorrect size!
    at com.diogosaraiva.starmade.wrapper.VersionManager.downloadUpdate(VersionManager.java:127)
    at com.diogosaraiva.starmade.wrapper.server.Server.<init>(Server.java:90)
    at com.diogosaraiva.starmade.wrapper.ServerWrapper.main(ServerWrapper.java:24)

这是一些VersionManager.java

final URL url = new URL(remotePath);
        final URLConnection connection = url.openConnection();
        final int size = connection.getContentLength();
        if (size < 0) {
            ServerWrapper.getLogger().info("Unable to get the latest version of StarMade!");
        } else {
            ServerWrapper.getLogger().info("Downloading the latest version of StarMade (length: " + size + " bytes, URL: " + remotePath + ")...");
        }
        inputStream = new BufferedInputStream(url.openStream());
        outputStream = new FileOutputStream("StarMade-latest.zip");
        final byte data[] = new byte[1024];
        int count;
        double sumCount = 0.0;
        int percentage;
        int lastPercentage = 0;
        while ((count = inputStream.read(data, 0, 1024)) != -1) {
            outputStream.write(data, 0, count);
            sumCount += count;
            percentage = (int) Math.ceil(sumCount / size * 100);
            if (percentage != lastPercentage) {
                ServerWrapper.getLogger().info(percentage + "%");
            }
            lastPercentage = percentage;
        }
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
        ServerWrapper.getLogger().info("Download finished. ");
        if (isInstalled() && backup) {
            ServerWrapper.getLogger().info("Backing up server.");
            final File f = new File(new SimpleDateFormat("'backup-'MM-dd hh-mm-ss-SS'.zip'").format(new Date()));
            if (!f.exists()) {
                f.createNewFile();
            }
            ZipUtils.zipDirectory(starmadeDirectory, f);
        }
        if (!starmadeDirectory.exists()) {
            starmadeDirectory.mkdirs();
        }
        ServerWrapper.getLogger().info("Installing update.");
        final File starmadeUpdate = new File("starmade-latest.zip");
        if (starmadeUpdate.length() != size) {
            throw new IOException("File downloaded is the incorrect size!");
        }

那么,为什么我会犯这个错误呢?

编辑:我以root用户身份运行,我运行的CentOS没有桌面环境。我已经尝试过openjdk和oracle jdk

问题是文件名在Linux上区分大小写。

outputStream = new FileOutputStream("StarMade-latest.zip");
...
final File starmadeUpdate = new File("starmade-latest.zip");

文件starmade-latest.zip可能甚至不存在于该目录中。因此,您的if (starmadeUpdate.length() != size)会将零与下载文件的长度进行比较。

检查文件名,问题就会消失。

最新更新