简体   繁体   English

使用 Apache Commons Net 下载后 FTP 文件损坏

[英]FTP file corrupt after download with Apache Commons Net

The files downloaded by this, are nearly the same size but differ in some lines.由此下载的文件大小几乎相同,但在某些行中有所不同。 Every answer points to binary file type.每个答案都指向二进制文件类型。 But this won't help.但这无济于事。 Got anybody an idea for the problem (transferring PDF)?有没有人对这个问题有想法(传输 PDF)?

FTPClient ftpClient = new FTPClient();
OutputStream outputStream = null;
boolean resultOk = true;

try {
    ftpClient.connect(host, port);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileTransferMode(FTP.COMPRESSED_TRANSFER_MODE);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    if (showMessages) {
        System.out.println(ftpClient.getReplyString());
    }
    resultOk &= ftpClient.login(usr, pwd);
    if (showMessages) {
        System.out.println(ftpClient.getReplyString());
    }

    outputStream = new FileOutputStream(localResultFile);
    resultOk &= ftpClient.retrieveFile(remoteSourceFile, outputStream);
    outputStream.flush();
    outputStream.close();

    if (showMessages) {
        System.out.println(ftpClient.getReplyString());
    }
    if (resultOk == true) {
        resultOk &= ftpClient.deleteFile(remoteSourceFile);
    }

    resultOk &= ftpClient.logout();
    if (showMessages) {
        System.out.println(ftpClient.getReplyString());
    }
} finally {

    ftpClient.disconnect();
}

It's clear from the files you have shared, that the transfer indeed happened in text/ascii mode.从您共享的文件中可以清楚地看出,传输确实是在 text/ascii 模式下进行的。

While probably not required by FTP specification, with some FTP servers (eg FileZilla server or ProFTPD), you cannot change transfer type before logging in. But servers like IIS, ProFTPD or vsftpd have no problem with that.虽然 FTP 规范可能没有要求,但对于某些 FTP 服务器(例如 FileZilla 服务器或 ProFTPD),您不能在登录前更改传输类型。但是像 IIS、ProFTPD 或 vsftpd 这样的服务器没有问题。 On the other hand FileZilla server defaults to binary mode anyway (what is another violation of the specification), so you are probably using yet another one.另一方面,FileZilla 服务器无论如何都默认为二进制模式(这是另一个违反规范的行为),因此您可能正在使用另一个。

In any case, move the .setFileType call after .login .无论如何,在.login之后移动.setFileType调用。 And test its return value.并测试其返回值。


And remove the .setFileTransferMode call.并删除.setFileTransferMode调用。 It does not do any harm with most servers, as hardly any server support MODE C , hence the call is ignored anyway.它不会对大多数服务器造成任何伤害,因为几乎没有任何服务器支持MODE C ,因此无论如何都会忽略调用。 But if you encounter a server that does, it would break the transfer, as FTPClient actually does not support it.但是,如果您遇到这样的服务器,它会中断传输,因为FTPClient实际上不支持它。

While my problem was related to corruption of the upload, I resolved similar issue by moving the set of file type after ftp login (I dont use transfer mode leaving it to its default value):虽然我的问题与上传损坏有关,但我通过在 ftp 登录后移动文件类型集解决了类似的问题(我不使用传输模式将其保留为默认值):

resultOk &= ftpClient.login(usr, pwd);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

I saw in some forums that setting binary file type before invoking login method, could lead to problems in transfer.我在一些论坛看到,在调用登录方法之前设置二进制文件类型,可能会导致传输问题。 Before this change, PDF file get downloaded but show corrupted fonts and elements.在此更改之前,下载 PDF 文件但显示损坏的字体和元素。 Now it works.现在它起作用了。 Hope it may helps someone.希望它可以帮助某人。

It seems to happen when using unescaped paths that contain spaces.使用包含空格的未转义路径时似乎会发生这种情况。 Eg C:/Documents and Settings/test例如 C:/Documents and Settings/test

Got it solved now by using a escaped path for the spaces.现在通过对空格使用转义路径来解决它。 Thanks for your help谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM