【发布时间】:2023-03-24 22:41:01
【问题描述】:
我在一遍又一遍地尝试让它工作后发布这个问题,但没有成功。 我尝试使用 apache commons 库在 android 中实现 FTP 文件传输。通信必须通过显式 TLS 身份验证来完成。 我可以成功登录,连接到服务器并列出文件,但是每当我尝试获取或存储文件时,我总是会遇到超时异常,超时值也非常大,即使对于 2Kb 的 txt 文件也是如此。 这是我的代码:
FTPSClient ftpClient = new FTPSClient("TLS", false);
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
KeyManagerFactory kmf = getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(null, null);
KeyManager km = kmf.getKeyManagers()[0];
ftpClient.setKeyManager(km);
ftpClient.setBufferSize(1024 * 1024);
ftpClient.setConnectTimeout(900000);
ftpClient.connect(InetAddress.getByName("server ip address"), 990);
// Set protection buffer size
ftpClient.execPBSZ(0);
// // Set data channel protection to private
ftpClient.execPROT("P");
ftpClient.login("user", "password");
ftpClient.changeWorkingDirectory("/");
ftpClient.setSoTimeout(900000);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
buffIn = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
//this works
FTPFile[] files = ftpClient.listFiles();
final OutputStream os = new FileOutputStream(finalStoragePath + "/OK.txt");
//this returns immediatly with false result
boolean getResult=ftpClient.retrieveFile("OK.txt", os);
//this always fail for timeout
boolean result = ftpClient.storeFile( picture.getName(), buffIn );
我找不到这种特定情况的任何示例,所有示例都是关于正常的 FTP 连接,我可以毫无问题地实现。你们中有人遇到过类似的问题吗?我真的需要一个解决方案,我必须尽快交付项目。
谢谢。
【问题讨论】:
标签: android ftp ftp-client apache-commons-net ftps