在Android中,如果你需要使用FTPS会话重用且遇到了没有sessionHostPortCache
字段的问题,你可以尝试以下解决方法:
确保你的Android项目中引入了org.apache.commons.net.ftp.FTPSClient
类。
创建一个自定义的FTPSClient
类,并覆盖_openDataConnection_()
方法。在该方法中,使用反射来设置sessionHostPortCache
字段。
下面是一个示例代码:
import org.apache.commons.net.ftp.FTPSClient;
import java.lang.reflect.Field;
import java.util.HashMap;
public class CustomFTPSClient extends FTPSClient {
@Override
protected void _openDataConnection_() throws IOException {
if (sessionHostPortCache != null) {
super._openDataConnection_();
return;
}
try {
Field field = FTPSClient.class.getDeclaredField("sessionHostPortCache");
field.setAccessible(true);
field.set(this, new HashMap());
super._openDataConnection_();
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
throw new IOException("Failed to set sessionHostPortCache field");
}
}
}
在上面的代码中,我们首先检查sessionHostPortCache
字段是否为null,如果不为null,则调用父类的_openDataConnection_()
方法。
如果sessionHostPortCache
字段为null,则使用反射来设置它为一个空的HashMap
对象,然后调用父类的_openDataConnection_()
方法。
CustomFTPSClient
类代替默认的FTPSClient
类。CustomFTPSClient ftpsClient = new CustomFTPSClient();
这样,你就可以在Android中使用FTPS会话重用,并解决没有sessionHostPortCache
字段的问题。