从 ftpClient的官方网的FAQ里面看到,实现这个需要用一个 自定义的SocketFactory
然后实现里面的 createSocket方法,有好多个。
http://wiki.apache.org/jakarta-commons/Net/FrequentlyAskedQuestions
原文如下:
Q: How can I set a connection timeout?
A: setDefaultTimeout does not set the connect timeout. It provides a default socket timeout. Only in J2SE 1.4 was the ability to specify a timeout on connect added to the Socket API. Since Commons Net 1.2.x has a J2SE 1.2 compatibility requirement, the ability to specify a connect timeout is not included. The way to workaround this is to implement your own SocketFactory and set it with SocketClient.setSocketFactory (FTPClient is a subclass of SocketClient). When you implement the SocketFactory, add a setConnectTimeout method or some such. Inside of the createSocket methods, use the J2SE 1.4 connect method with the timeout. We could actually provide socket factory that subclasses DefaultSocketFactory to do this without breaking backward compatibility, but that would have to be discussed further. The way to do it is to compile it only if J2SE >= 1.4 is being used. SocketClient could check for availability of the J2SE 1.4 connect method and instantiate the J2SE >= 1.4 factory if available (using Class.forName and newInstance). The setDefaultTimeout method could then be changed to also set the connect timeout in the new factory if being used. If users want this functionality enough, the best chance of getting it implemented soon is to submit a patch.
不过我看了 ftpClient 2.0的源代码。
ftpClient.connect(hostName, 21);
看看 connect的代码如下
public void connect(String hostname, int port)
throws SocketException, IOException
{
_socket_= _socketFactory_.createSocket();
_socket_.connect(new InetSocketAddress(hostname, port), connectTimeout);
_connectAction_();
}
其中有一个connectTimeout正是我们需要的连接超时,我们看看它的定义
/** The socket's connect timeout (0 = infinite timeout) */
private static final int DEFAULT_CONNECT_TIMEOUT = 0;
protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
可见默认是不限超时的。再搜索一下,找到如下这个方法
/**
* Sets the connection timeout in milliseconds, which will be passed to the {@link Socket} object's
* connect() method.
* @param connectTimeout The connection timeout to use (in ms)
* @since 2.0
*/
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
/**
* Get the underlying socket connection timeout.
* @return
* @since 2.0
*/
public int getConnectTimeout() {
return connectTimeout;
}
可见,我们完全可以直接设置超时时间就行了。具体代码如下:
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(1000); // 一秒钟,如果超过就判定超时了
ftpClient.connect(hostName, 21);
估计2,0这个版本对这个问题进行了完善和增强,可以这么简单的实现了。
相关推荐
使用apache的commons-pool2 构建 FTPClient连接池 有FtpClientFactory、FtpClientPool、FtpConfig、FtpOperate 四个类组成 还有ftp连接池的一些配置参数信息在ftp.properties文件中 注释完整欢迎大家下载使用
1 博文链接:https://bai2mumu.iteye.com/blog/2231916
主要介绍了Spring Boot整合FTPClient线程池的实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Java FTPClient连接池的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
FTPClient的jar包 FTPClient ftpClient = new FTPClient(); ftpClient.connect("ftp.foo.com"); ftpClient.login("user01", "pass1234"); ftpClient.download("C:\\Temp\\&quo; t;, "README.txt"); // ...
使用JDK1.8、SpringBoot2.2.10.RELEASE、lombok1.18.8、guava23.0、hutool5.3.10、commons-pool2 2.7.0、tika1.22等实现多Ftp连接池实现,通过守护线程实现连接池内连接可用性校验,配置最大、最小连接个数防止Ftp...
springboot集成ftp连接池工具,很好的解决了并发大用户上传附件慢的问题
ftpclient 未完成的作品
sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP
ftpclient开发所需要的jar包,commons-net-1.4.1.zip,jakarta-oro-2.0.8.zip
C#的FTP操作类库,支持FTP...如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用。 那就是System.Net.FtpClient,链接地址:https://netftp.codeplex.com
CE FTPClient
ftpclient用于实现java的文件上传和下载
用于java开发FTP客户端,很方便,而且里面有api
FtpClient写的java FTP工具FtpClient写的java FTP工具FtpClient写的java FTP工具
FTPClient (apache) 上传下载示例代码
编译后可在bin下面生成System.Net.FtpClient.dll,用于C#在ftp上传时调用。
ftpClient.changeWorkingDirectory(path); ftpClient.enterLocalPassiveMode(); //由于apache不支持中文语言环境,通过定制类解析中文日期类型 ftpClient.configure(new FTPClientConfig(...
FtpClient FTP 操作类 支持上传下下传(含多线程下载与上传)
FTPClient.jar 帮助你完成FTP的有关开发。 FTPClient.jar 帮助你完成FTP的有关开发。 FTPClient.jar 帮助你完成FTP的有关开发。