Zip 文件上传到服务器时损坏

发布于 2024-09-28 05:39:36 字数 1718 浏览 5 评论 0原文

我的 java 程序将 zip 文件从我的系统上传到 FTP 服务器。 uploadfile() 是一个包含上传代码的函数。

uploadfile("192.168.0.210","muruganp","vm4snk","/home/Admin/GATE521/LN_RB_Semivalid2junk/Output/"+date+"_RB1.zip","/fileserver/filesbackup/Emac/"+日期+“_RB1.zip”);

public static boolean uploadfile(String server, String username,
        String Password, String source_file_path, String dest_dir) {
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(server);
        ftp.login(username, Password);
        System.out.println("Connected to " + server + ".");
        System.out.print(ftp.getReplyString());
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            return false;
        }
        System.out.println("FTP server connected.");
        InputStream input = new FileInputStream(source_file_path);
        ftp.storeFile(dest_dir, input);
        System.out.println(ftp.getReplyString());
        input.close();
        ftp.logout();
    } catch (Exception e) {
        System.out.println("err");
        e.printStackTrace();
        return false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (Exception ioe) {}
        }
    }
    return true;
}

我系统中的 zip 文件是完美的。但是在服务器位置上传相同的内容后,下载相同的内容并解压就会出现问题。错误提示“文件已损坏”。我应该怎么做才能解决这个问题。请就此提出建议。

我怀疑问题可能是通过 ASCII 模式传输。它实际上应该按照这个问题通过二进制模式传输。如何达到同样的效果?请指教。

My java program uploads a zip file from my system to FTP server. uploadfile() is a function that contains the uploading code.

uploadfile("192.168.0.210","muruganp","vm4snk","/home/Admin/GATE521/LN_RB_Semivalid2junk/Output/"+date+"_RB1.zip","/fileserver/filesbackup/Emac/"+date+"_RB1.zip");

public static boolean uploadfile(String server, String username,
        String Password, String source_file_path, String dest_dir) {
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(server);
        ftp.login(username, Password);
        System.out.println("Connected to " + server + ".");
        System.out.print(ftp.getReplyString());
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            return false;
        }
        System.out.println("FTP server connected.");
        InputStream input = new FileInputStream(source_file_path);
        ftp.storeFile(dest_dir, input);
        System.out.println(ftp.getReplyString());
        input.close();
        ftp.logout();
    } catch (Exception e) {
        System.out.println("err");
        e.printStackTrace();
        return false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (Exception ioe) {}
        }
    }
    return true;
}

The zip file that I do have in my system is perfect. But after uploading the same in the server location,downloading the same, and extracting the problem occurs. "The file is corrupt" says the error. What should I do to resolve this issue. Kindly advise on this.

I suspect the problem would be something like, transferring through ASCII mode. It should actually be transferred through binary mode as per this QUESTION. How to attain the same? Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软甜啾 2024-10-05 05:39:36

最好的猜测是 FTP 上传使用的是 ascii 模式,这会损坏 zip 等二进制文件。验证这一点,如果是这样,请将其更改为二进制模式。

Best guess is that the FTP upload is using ascii mode which will corrupt a binary file like a zip. Verify this and if so change it to binary mode instead.

老旧海报 2024-10-05 05:39:36

使用 FTPClient 的 setFileType 方法上传前将其设置为 FTP.BINARY_FILE_TYPE

Use the setFileType method of FTPClient to set it to FTP.BINARY_FILE_TYPE before uploading

揽月 2024-10-05 05:39:36

我只是使用 setFileType(FTP.BINARY_FILE_TYPE) 来解决它。
这些信息真的很有帮助!多谢。

I just used setFileType(FTP.BINARY_FILE_TYPE) to solve it.
Those information are really helpful! Thanks a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文