gpt4 book ai didi

java - Apache FTP 客户端

转载 作者:行者123 更新时间:2023-12-01 12:39:02 26 4
gpt4 key购买 nike

我正在使用 Apache 的库编写一个非常简单的 FTPClient 应用程序。

我所做的就是从服务器下载一个 jar 文件。没有异常(exception),当我在 MacOS 上运行时,该应用程序工作正常,一旦我在 Windows 中运行相同的代码,下载的文件就会比服务器上的文件小。不过,没有异常(exception),一切似乎都很好。

我要疯了,我正在使用二进制模式,代码是如此简单,我不敢相信我从昨天开始就一直坚持下去!!!

请帮忙!

public boolean loadLatest(){
FTPClient ftp = new FTPClient();
try {
ftp.connect("server address");
ftp.setControlKeepAliveTimeout(300);
ftp.enterLocalPassiveMode();
String fu = "username";
ftp.login(fu, "password");
int reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
FTPFilter filter = new FTPFilter();
FTPFile[] finfo = ftp.listFiles(".",filter);
if (finfo.length==0){
return false;
}

File currentJar = new File("sm.jar");

FileOutputStream output;
output = new FileOutputStream("sm.jar");
if (ftp.retrieveFile(finfo[0].getName(), output)==false){
System.out.println("Bad file!");
}
output.close();
ftp.logout();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}

这是我的代码:

谢谢

最佳答案

您可能滥用了 Apache API。

public boolean setFileTransferMode(int mode) throws IOException

Sets the transfer mode. The default transfer mode FTP.STREAM_TRANSFER_MODE if this method is never called or if a connect method is called.

可能的值为STREAM_TRANSFER_MODESTREAM_TRANSFER_MODEBLOCK_TRANSFER_MODE

所以你的下面一行:

ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

API 的使用是否有效,并且未将客户端设置为在 BINARY 模式下工作。

您所说的FTP传输模式实际上是由另一种方法控制的:

public boolean setFileType(int fileType) throws IOException

Sets the file type to be transferred. This should be one of FTP.ASCII_FILE_TYPE , FTP.BINARY_FILE_TYPE, etc. The file type only needs to be set when you want to change the type. After changing it, the new type stays in effect until you change it again. The default file type is FTP.ASCII_FILE_TYPE if this method is never called. The server default is supposed to be ASCII (see RFC 959), however many ftp servers default to BINARY. To ensure correct operation with all servers, always specify the appropriate file type after connecting to the server.

N.B. currently calling any connect method will reset the type to FTP.ASCII_FILE_TYPE.

最有可能的是,您需要使用最后一个方法将模式更改为BINARY,并且您的传输应该没问题。

关于java - Apache FTP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com