﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-我的博客-随笔分类-FTP</title><link>http://www.blogjava.net/qbna350816/category/55061.html</link><description>201103</description><language>zh-cn</language><lastBuildDate>Sat, 25 Jun 2016 00:44:16 GMT</lastBuildDate><pubDate>Sat, 25 Jun 2016 00:44:16 GMT</pubDate><ttl>60</ttl><item><title>ftp4j 1.7 手册</title><link>http://www.blogjava.net/qbna350816/archive/2016/06/21/430984.html</link><dc:creator>胡小军</dc:creator><author>胡小军</author><pubDate>Tue, 21 Jun 2016 14:34:00 GMT</pubDate><guid>http://www.blogjava.net/qbna350816/archive/2016/06/21/430984.html</guid><wfw:comment>http://www.blogjava.net/qbna350816/comments/430984.html</wfw:comment><comments>http://www.blogjava.net/qbna350816/archive/2016/06/21/430984.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qbna350816/comments/commentRss/430984.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qbna350816/services/trackbacks/430984.html</trackback:ping><description><![CDATA[<div style="font-family: 微软雅黑; background-color: #ffffff;"><div style="background-color: inherit;"><div style="background-color: inherit;"><div><div style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="font-size: 16px; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial; background-color: inherit;">原文：</span></span></span></span><a href="http://www.sauronsoftware.it/projects/ftp4j/manual.php" style="cursor: pointer; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">http://www.sauronsoftware.it/projects/ftp4j/manual.php</span></a></div></div><div></div><div><h2><span style="font-size: 12pt; font-family: Arial;">要求</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要运行ftp4j library，你需要Java 运行时环境v. 1.4+.</span></p><h2><span style="font-size: 12pt; font-family: Arial;">安装</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">将ftp4j JAR文件添加到你应用程序的classpath中, 然后你就可以自动启用ftp4j类的使用了.</span></p><h2><span style="font-size: 12pt; font-family: Arial;">Javadocs</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">可参考</span><a href="http://www.sauronsoftware.it/projects/ftp4j/api/index.html" style="cursor: pointer; color: #005fa9; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">ftp4j javadocs</span></a><span style="font-size: 12pt; font-family: Arial;">.</span></p><h2><span style="font-size: 12pt; font-family: Arial;">快速入门</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">包中的主类是</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPClient</em>).</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">创建一个</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient</em>&nbsp;实例：</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">FTPClient client = new FTPClient();</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">连接远程FTP服务:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.connect("ftp.host.com");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果服务端口不是标准的21端口 (或 FTPS的990端口)，需要使用port参数进行指定:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.connect("ftp.host.com", port);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.connect("ftp.host.com", 8021);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">然后进行登录流程:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.login("carlo", "mypassword");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果没有抛出任何异常的话，那么你就通过远程服务器的认证了.否则，如果验证失败，你将会收到</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">it.sauronsoftware.ftp4j.FTPException异常</em>.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">匿名认证，如果被连接服务认可的话， 可通过发送用户名"anonymous" 和任意密码来完成(注意，有些服务器需要e-mail地址来代替密码):</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.login("anonymous", "ftp4j");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">使用远程FTP服务来做任何事情，然后再断开连接:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.disconnect(true);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">这会向远程器发送FTP QUIT命令, 以进行一个合法断开流程.如果你只是想中断连接而不想向服务器发送任何通知，那么可以使用:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.disconnect(false);</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">使用代理进行连接</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">客户端通过连接器（一个继承自</span><em style="font-size: 12pt; font-family: Arial;">it.sauronsoftware.ftp4j.FTPConnector的对象</em>）来连接服务器, 它将返回一个已经打开的连接(一个实现了<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPConnection</em>&nbsp;接口的对象).这也是为什么ftp4j 可以支持大量代理的原因.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在连接远程服务器前，客户端实例可以使用s</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">etConnector()</em>&nbsp;方法来设置连接器:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setConnector(anyConnectorYouWant);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果没有设置连接器的话，会使用默认的连接器</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">DirectConnector</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.connectors.DirectConnector</em>), 它实现了对远程服务器的直接连接，且不会使用代理。</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果你只能通过代理来连接远程服务器, ftp4j包可以让你在下面的连接器中进行选择:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">HTTPTunnelConnector</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.connectors.HTTPTunnelConnector</em>)<br style="background-color: inherit;" />它可以通过HTTP代理来进行连接，并支持CONNECT方法.</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPProxyConnector</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.connectors.FTPProxyConnector</em>)<br style="background-color: inherit;" />它可以通过FTP代理进行连接,支持SITE和OPEN命令风格的苛刻远程主机连接.其它类型的FTP代理,需要<em style="background-color: inherit;">username@remotehost</em>&nbsp;认证，且可以不使用连接器，因为它们对于客户端来说是透明的。</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">SOCKS4Connector</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.connectors.SOCKS4Connector</em>)<br style="background-color: inherit;" />它可以通过SOCKS 4/4a代理进行连接.</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">SOCKS5Connector</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.connectors.SOCKS5Connector</em>)<br style="background-color: inherit;" />它可以通过SOCKS 5代理进行连接.</li></ul><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">因为ftp4j的连接器架构设计为可插拔的，因此你可以继承</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPConnector</em>&nbsp;抽象类来构建自己的连接器.</p><h2><span style="font-size: 12pt; font-family: Arial;">FTPS/FTPES 安全连接</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">ftp4j包支持FTPS (隐式基于 TLS/SSL的FTP) 和FTPES (显示基于TLS/SSL的FTP).</span></p><p style="margin-right: 0px; margin-left: 0px;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">setSecurity()</em>&nbsp;方法可用来打开这种特性:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setSecurity(FTPClient.SECURITY_FTPS); // 启用 FTPS</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setSecurity(FTPClient.SECURITY_FTPES); // 启用 FTPES</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">两个方法都需要在连接远程服务器前调用.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果安全协议设置成了</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">SECURITY_FTPS</em>, 则<em>connect()</em>&nbsp;方法默认使用的端口为990.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">默认情况下，客户端对象商讨SSL连接会使用</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">javax.net.ssl.SSLSocketFactory.getDefault()作为其套接字工厂</em>.可通过调用client.<em style="background-color: inherit;">setSSLSocketFactory()方法来改变默认套接字工厂</em>. 另外一种<em style="background-color: inherit;">SSLSocketFactory</em>, 可用来信任远程服务器颁发的证书(谨慎使用):</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">import it.sauronsoftware.ftp4j.FTPClient;<br /> import java.security.KeyManagementException; <br />import java.security.NoSuchAlgorithmException;<br /> import java.security.SecureRandom; <br />import java.security.cert.X509Certificate;<br /> import javax.net.ssl.SSLContext;<br /> import javax.net.ssl.SSLSocketFactory; <br />import javax.net.ssl.TrustManager; <br />import javax.net.ssl.X509TrustManager;  <br />// ...  TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { 	<br />public X509Certificate[] getAcceptedIssuers() { 		return null; 	<br />} 	<br />public void checkClientTrusted(X509Certificate[] certs, String authType) { 	} 	<br />public void checkServerTrusted(X509Certificate[] certs, String authType) { 	} } }; <br />SSLContext sslContext = null;<br />try { 	sslContext = SSLContext.getInstance("SSL"); 	<br />sslContext.init(null, trustManager, new SecureRandom()); <br />} catch (NoSuchAlgorithmException e) { 	<br />e.printStackTrace(); <br />} catch (KeyManagementException e) { <br />	e.printStackTrace(); <br />} <br />SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); <br />FTPClient client = new FTPClient(); <br />client.setSSLSocketFactory(sslSocketFactory); <br />client.setSecurity(FTPClient.SECURITY_FTPS); <br />// or client.setSecurity(FTPClient.SECURITY_FTPES);  // ...</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">浏览远程站点</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">获取当前目录的的绝对路径（此目录是FTP服务器的home目录）:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">String dir = client.currentDirectory();</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">改变目录:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.changeDirectory(newPath);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你可以使用绝对路径和相对路径:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.changeDirectory("/an/absolute/one"); <br />client.changeDirectory("relative");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">回到父目录:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.changeDirectoryUp();</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">重命名文件和目录</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要重命名远程文件或目录:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.rename("oldname", "newname");</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">移动文件和文件家</span></h2><p style="margin-right: 0px; margin-left: 0px;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">rename()</em>&nbsp;方法也可以用来从当前位置移动文件或目录到其它位置.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在这个例子子，假设在当前工作目录中，你有一个名为"myfile.txt"的文件,然后你想将其移动到子目录"myfolder"中:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.rename("myfile.txt", "myfolder/myfile.txt");</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">删除文件</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要删除远程文件，需要调用:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.deleteFile(relativeOrAbsolutePath);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在这个例子中:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.deleteFile("useless.txt");</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">创建、删除目录</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果远程服务给你机会的话，你可以在远程站点上创建新目录:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.createDirectory("newfolder");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你也可以已存在的目录:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.deleteDirectory(absoluteOrRelativePath);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在这个例子中:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.deleteDirectory("oldfolder");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">请注意，通常情况下，FTP 服务器只允许删除空目录.</span></p><h2><span style="font-size: 12pt; font-family: Arial;">列出文件、目录、连接</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">FTP 协议并不会提供大量支持方法来获取工作目录的完整信息.通常LIST命令会给你想知道的东西，但不辛的是，每个服务器会使用不同样式的响应. 这意味着某些服务器会返回UNIX样式的目录，有些服务器会返回DOS样式的目录,其它的服务器又会使用别的样式.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">ftp4j 包可以处理许多的LIST响应格式,&nbsp;</span><span style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">并将它们构建成统一目录内容的结构对象表示</span><span style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">.当前ftp4j可以处理:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">UNIX&nbsp;样式及其变种(如MAC样式)</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">DOS&nbsp;样式</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">NetWare&nbsp;样式</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">EPLF</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">MLSD</span></li></ul><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">这可以通过使用可插拔的parsers来完成.包</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">it.sauronsoftware.ftp4j.listparsers包含了用于处理上述样式的对象</em>.大多数时间，这些已经够用了。</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要列出当前工作目录下的文件或文件夹，可调用:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">FTPFile[] list = client.list();</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果你收到了</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPListParseException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPListParseException</em>) 异常，这就意味着服务器对LIST命令返回了不可理解的样式,即它不是上述列出的样式.因此，你可以尝试使用<em style="background-color: inherit;">listNames()</em>&nbsp;方法， 但它并不如<em style="background-color: inherit;">list()方法有优势</em>.。为了弥补这种缺陷，你可以构建你自己的<span style="line-height: 1.5; background-color: inherit;">LIST响应解析器,以支持你遇到的样式.你可以实现</span><em style="line-height: 1.5; background-color: inherit;">FTPListParser</em><span style="line-height: 1.5; background-color: inherit;">&nbsp;(</span><em style="line-height: 1.5; background-color: inherit;">it.sauronsoftware.ftp4j.FTPListParser</em><span style="line-height: 1.5; background-color: inherit;">) 接口，然后你可以在client的</span><em style="line-height: 1.5; background-color: inherit;">addListParser()方法使用此实现</em><span style="line-height: 1.5; background-color: inherit;">.</span></p><p style="margin-right: 0px; margin-left: 0px;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPFile</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPFile</em>) 对象提供了目录内容的表示，包括文件，子目录和连接. 根据服务器的响应，FTPFile对象的某些字段可以是<em style="background-color: inherit;">null</em>&nbsp;的或者是无意义的.请检查javadocs来了解细节.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">list()</em>&nbsp;方法中你也可以使用文件过滤参数,如：</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">FTPFile[] list = client.list("*.jpg");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果连接服务器明确支持MLSD命令, ftp4j会用其来代替基本的LIST命令。MLSD的响应事实更为标准，准确，更易解析.不幸的是，不是所有服务器都支持这个命令，并且有些服务器支持得非常糟糕.基于这些理由，开发者可以控制ftp4j是否应该使用MLSD命令，即通过调用</span><em style="font-size: 12pt; font-family: Arial;">FTPClient对象的</em><em style="background-color: inherit;">setMLSDPolicy()方法</em>. 合法的值:</p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient.MLSD_IF_SUPPORTED</em><br style="background-color: inherit;" />client只在服务器明确支持MLSD命令时，才使用MLSD命令. 这是ftp4j默认的行为.</p></li><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient.MLSD_ALWAYS</em><br style="background-color: inherit;" />client总是会使用MLSD命令, 即便服务器没有明确表明支持MLSD命令.</p></li><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient.MLSD_NEVER</em><br style="background-color: inherit;" />client绝不使用MLSD命令,即便服务器明确表明支持MLSD命令.</p></li></ul><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">例如：</span></p><code style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setMLSDPolicy(FTPClient.MLSD_NEVER);</span></code><h2><span style="font-size: 12pt; font-family: Arial;">获取文件、目录的最后修改时间</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">通常情况下</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPFile对象会告诉你条目的最后修改时间</em>, 但正如上面描述的，这依赖于服务器发回的响应.如果你需要最后的修改时间，但你又不能通过list()方法得到，那么可以尝试这样做：</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">java.util.Date md = client.modifiedDate("filename.ext");</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">下载、上传文件</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">下载远程文件最简单的方式是调用</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">download(String, File)</em>&nbsp;方法：</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.download("remoteFile.ext", new java.io.File("localFile.ext"));</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要上传:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.upload(new java.io.File("localFile.ext"));</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要在已有文件中上传追加内容:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.append(new java.io.File("localFile.ext"));</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">这些是阻塞式调用:它们会在传输完成后(或failed, 或 aborted时)才返回. 此外同步锁是否由客户端来实施的，因为在每个时间段内只允许有一个常规的FTP通信.在每个时间段内，你可以处理多个传输器，即使用多个</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPClient</em>&nbsp;对象,每个都与服务器建立一个私有连接.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你可以使用</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPDataTransferListener</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPDataTransferListener</em>)对象来监控传输.你可以自己实现一个:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">import it.sauronsoftware.ftp4j.FTPDataTransferListener;  <br />public class MyTransferListener implements FTPDataTransferListener {  	<br />public void started() { 		<br />// Transfer started 	<br />}  	<br />public void transferred(int length) { 		<br />// Yet other length bytes has been transferred since the last time this 		<br />// method was called 	<br />}  	<br />public void completed() { 		<br />// Transfer completed 	<br />}  	<br />public void aborted() { 		<br />// Transfer aborted 	<br />}  	<br />public void failed() { 		<br />// Transfer failed 	<br />}  <br />}</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">现在像下面这样来下载或上传:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.download("remoteFile.ext", new java.io.File("localFile.ext"), new MyTransferListener());</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.upload(new java.io.File("localFile.ext"), new MyTransferListener());</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.append(new java.io.File("localFile.ext"), new MyTransferListener());</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">当client处理下载或上传时，传输器可以被同一个</span><em style="font-size: 12pt; font-family: Arial;">FTPClient对象的不同线程通过调用</em>&nbsp;<em style="background-color: inherit;">abortCurrentDataTransfer()</em>&nbsp;方法<span style="line-height: 1.5; background-color: inherit;">aborted</span>. 此方法还需要一个boolean参数:<em style="background-color: inherit;">true表示执行合法的</em>abort过程(即向服务器发送ABOR命令),&nbsp;<em style="background-color: inherit;">false表示实然关闭传输器，而不向服务器发送通知</em>:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.abortCurrentDataTransfer(true); // Sends ABOR</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.abortCurrentDataTransfer(false); // Breaks abruptly</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">需要注意的是，</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">list()和</em><em style="background-color: inherit;">listNames()</em>&nbsp;方法暗中包含了数据传输器，因<em style="background-color: inherit;">abortCurrentDataTransfer()</em>&nbsp;方法也可以用来中断其list过程.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">当数据传输器在</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">download()</em>,&nbsp;<em style="background-color: inherit;">upload()</em>,&nbsp;<em style="background-color: inherit;">append()</em>,&nbsp;<em style="background-color: inherit;">list()</em>&nbsp;and&nbsp;<em style="background-color: inherit;">listNames()</em>&nbsp;方法中中断时，将会抛出<em style="background-color: inherit;">FTPAbortedException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPAbortedException</em>).</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">下载和上传操作可通过</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">restartAt&nbsp;</em>参数来重新恢复:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.download("remoteFile.ext", new java.io.File("localFile.ext"), 1056);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">此操作会文件的第1056个字节处继续执行下载操作。第一个传输的字节将是第1057个.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">其它&nbsp;</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">download()</em>,&nbsp;<em style="background-color: inherit;">upload()</em>&nbsp;和<em style="background-color: inherit;">append()方法的变种可以让你使用流来替代</em><em style="background-color: inherit;">java.io.File对象</em>.因此你可以在数据库，网络连接或其它流上来传输数据。</p><h2><span style="font-size: 12pt; font-family: Arial;">Active 、 passive 数据传输模式</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">客户端和服务器之间的数据传输通道是通过单独的网络连接来建立的. 在传输通道建立期间，服务器可以是</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">active或</em><em style="background-color: inherit;">passive的</em>. 服务器激活数据传输时，工作如下：</p><ol data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">client向服务器发送其IP地址和端口号.</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">client向服务器发送数据传输请求，并在之前发送的端口上启动监听.</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">服务器使用客户端提供的地址和端口来连接客房端.</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">数据传输将在新建立的通道中进行.</span></li></ol><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">active模式需要你的client能够收到来自服务器的连接.如果你的client处于防火墙， 代理或这两者混合之后，那么大部分时间都会出现问题，因为它不能收到来外界的连接. 下面是</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">passive数据传输模式</em>：</p><ol data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">client要求服务器准备好一个passive数据传输.</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">服务器使用其IP地址和端口号进行响应.</span></li><li style="background-color: inherit;"><span style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">client请求传输和连接.</span></li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">数据传输将在新建立的通道中进行.</span></li></ol><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在passive模式中，客户端连接不要求能收到服务器的连接请求.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在ftp4j中，你可以使用下面的调用来切换active、passive模式:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setPassive(false); // Active mode</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setPassive(true); // Passive mode</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">ftp4j client passive 标志的默认值为</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">true</em>: 如果你没有调用<em style="background-color: inherit;">setPassive(false)</em>&nbsp;，你的客户端在每次传输前，都会向服务器请求passive模式.</p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">当使用 passive文件传输时，服务器会提供一个 IP地址和一个端口号.作为FTP规范的client,需要使用给定的主机号和端口进行连接.在商业环境中，这种行为可能会经常带来问题，因为NAT配置可能会阻止对IP地址的连接.这就是为什么FTP clients通常会忽略服务器返回的任何IP地址，进而在通信线路中使用同样的主机来连接服务器.ftp4j的行为依赖于服务器因素:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">每个</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPConnector</em>&nbsp;都有其默认行为.大部分连接器都会忽略服务器返回的IP地址。目前，默认使用返回地址的连接器是<em style="background-color: inherit;">FTPProxyConnector</em>.</li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">连接器的行为可通过定义名为</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">ftp4j.passiveDataTransfer.useSuggestedAddress的系统属性来覆盖。如果设置为</em>"true", "yes" 或"1",所有连接器都会使用服务器返回的地址，反之，如果将其设置为"false", "no" or "0", 所有服务器都不会使用返回的地址.</li><li style="background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">最后,连接器的默认行为和全局设置都可以在任何连接器实例中进行覆盖。你可通过获取客房端连接器，并调用其</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">setUseSuggestedAddressForDataConnections()</em>&nbsp;方法来达到目的.</li></ul><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在active传输模式中，可以设置下面的系统属性:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">ftp4j.activeDataTransfer.hostAddress</em><br style="background-color: inherit;" />主机地址.当服务器请求执行与客户端连接时，client会跳转到给定地址的服务器. 此值应该是一个有效的IPv4地址，如：<em style="background-color: inherit;">178.12.34.167</em>. 如果没有提供此值，客户端会自动解析系统地址.但如果client运行于LAN中，为了激活数据传输，将会使用带端口转发的路由器来连接外部服务器,那么自动探测到的地址可能不是正确的. 当系统有多个网络接口时，也可能发生这种情况.通常使用系统属性，可以解决这种问题</p></li><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">ftp4j.activeDataTransfer.portRange</em><br style="background-color: inherit;" />连接端口范围. client会在其中挑选一个来进行数据传输.此值 必须是<em style="background-color: inherit;">start-stop</em>&nbsp;形式 ，如<em style="background-color: inherit;">6000-7000</em>&nbsp;表示client只会在给定范围内挑选一个端口来连接服务器.默认情况下没有指定端口范围:这表示client会挑选任何一个可用的端口.</p></li><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">ftp4j.activeDataTransfer.acceptTimeout</em><br style="background-color: inherit;" />以毫秒为单位的连接超时时间. 如果服务器不能在给定超时时间内连接client，传输会因<em style="background-color: inherit;">FTPDataTransferException异常而中断</em>.0值表示永不超时。默认值30000 (即30秒).</p></li></ul><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">要设置系统属性，你可以:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">用一个或多个&nbsp;</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">-Dproperty=value参数来启动JVM</em>.如:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">java -Dftp4j.activeDataTransfer.hostAddress=178.12.34.167      -Dftp4j.activeDataTransfer.portRange=6000-7000      -Dftp4j.activeDataTransfer.acceptTimeout=5000 MyClass</span></pre></li><li style="background-color: inherit;"><p style="margin-right: 0px; margin-left: 0px; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">直接在代码中设置系统属性，如:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace; background-color: inherit;"><span style="font-size: 12pt; font-family: Arial;">System.setProperty("ftp4j.activeDataTransfer.hostAddress", "178.12.34.167"); <br />System.setProperty("ftp4j.activeDataTransfer.portRange", "6000-7000"); <br />System.setProperty("ftp4j.activeDataTransfer.acceptTimeout", "5000");</span></pre></li></ul><h2><span style="font-size: 12pt; font-family: Arial;">二进制和文本数据传输类型</span></h2><h2><span style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">数据传输的另一个核心概念是</span><em style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">binary</em><span style="line-height: 1.5; background-color: inherit;">&nbsp;和</span><em style="line-height: 1.5; background-color: inherit;">textual</em><span style="line-height: 1.5; background-color: inherit;">&nbsp;类型.当传传输的文件是二进制文件时，它将视为二进制流，服务器会按原样存储。而文本数据传输会将传输的文件视为字符流,会进行字符集转换. 假设你的client正运行Windows平台上，而服务器运行UNIX上,它们的默认字符集通常是不同的. client以文本类型来发送文件时，client会假设文件是按机器标准字符集来编码的，因此在发送前，它会解码每个字符并将其编码为 中间字符集. 服务器收到流后，在存储前，会解码中间字符集，并将其编码为机器默认的字符集.字节虽然被改变了，但内容是相同的。</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你可以调用下面的方法选择你传输的类型:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setType(FTPClient.TYPE_TEXTUAL);</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setType(FTPClient.TYPE_BINARY);</span></pre><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setType(FTPClient.TYPE_AUTO);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">默认的TYPE_AUTO常量</em>&nbsp;,会让client自动来挑选类型:如果文件的扩展名是client能被识别的文本类型标记，那么它会选择文本传输器来执行. 文件扩展名是通过<em style="background-color: inherit;">FTPTextualExtensionRecognizer</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPTextualExtensionRecognizer</em>) 实例来识别的. 默认扩展识别器是<em style="background-color: inherit;">it.sauronsoftware.ftp4j.recognizers.DefaultTextualExtensionRecognizer</em>, 会将下面的视为文本类型:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">abc acgi aip asm asp c c cc cc com conf cpp</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">csh css cxx def el etx f f f77 f90 f90 flx</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">for for g h h hh hh hlb htc htm html htmls</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">htt htx idc jav jav java java js ksh list</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">log lsp lst lsx m m mar mcf p pas php pl pl</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">pm py rexx rt rt rtf rtx s scm scm sdml sgm</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">sgm sgml sgml sh shtml shtml spc ssi talk</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">tcl tcsh text tsv txt uil uni unis uri uris</span><br style="background-color: inherit;" /><span style="font-size: 12pt; font-family: Arial;">uu uue vcs wml wmls wsc xml zsh</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你可通过实现</span><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPTextualExtensionRecognizer</em>&nbsp;接口来实现你自己的识别器，但你可以更喜欢使用 class&nbsp;<em style="background-color: inherit;">ParametricTextualExtensionRecognizer</em>(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.recognizers.ParametricTextualExtensionRecognizer</em>)便利类.<br />无论如何，都不要忘记将你的识别器设置在client中:</p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setTextualExtensionRecognizer(myRecognizer);</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">数据传输压缩</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">有些服务器支持数据传输压缩特性－MODE Z. 在传输大文件时，此特性可以节省带宽.一旦client连上服务器并通过认证,就可通过调用下面的方法来检查是否支持压缩：</span></p><code style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">boolean compressionSupported = client.isCompressionSupported();</span></code><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">如果服务器端支持压缩,就可通过下面的调用来启用压缩:</span></p><code style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setCompressionEnabled(true);</span></code><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">在此调用之后，后续的数据传输(下载，上传，列举操作）都被将压缩以节省带宽.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">数据传输压缩可通过下面的调用来禁用:</span></p><code style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setCompressionEnabled(false);</span></code><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">也可以检查标记值:</span></p><code style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">boolean compressionEnabled = client.isCompressionEnabled();</span></code><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">请注意：压缩数据传输只当压缩支持且启用了的情况下才会发生.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">默认情况下，压缩是禁用的，即便是服务器支持压缩. 如果有需要,可以显示地打开.</span></p><h2><span style="font-size: 12pt; font-family: Arial;">不做任何事（NOOPing the server）</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">假设你的client什么事情都不做，因为在等待用户输入. 通常情况下， FTP服务器会自动断开非活跃客户端. 为了避免超时，你可以发送NOOP命令.<br />此命令不会做任何事情，但它会向服务器说明：客户端仍然还活着，请重围超时计数器.调用如下:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.noop();</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">当非活跃超时发生时，客户端也可以自动发送NOOPs. 默认情况下，此特性是禁用的。它可以在&nbsp;</span><em style="line-height: 1.5; font-size: 12pt; font-family: Arial; background-color: inherit;">setAutoNoopTimeout()</em><span style="line-height: 1.5; background-color: inherit;">&nbsp;方法中设置超时时间时启用,并提供一个毫秒为单位的值.如:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setAutoNoopTimeout(30000);</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">使用此值，client会在30秒后发送一个NOOP命令.</span></p><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">NOOP超时可通过设置小于等于0的值来禁用:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">client.setAutoNoopTimeout(0);</span></pre><h2><span style="font-size: 12pt; font-family: Arial;">网站特殊的自定义命令</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你可以像下面这样来发送站点特殊命令：</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">FTPReply reply = client.sendSiteCommand("YOUR COMMAND");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">你也可以发送自定义命令:</span></p><pre style="font-family: Monaco, Consolas, Courier, 'Lucida Console', monospace;"><span style="font-size: 12pt; font-family: Arial;">FTPReply reply = client.sendCustomCommand("YOUR COMMAND");</span></pre><p style="margin-right: 0px; margin-left: 0px;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">sendSiteCommand()</em>&nbsp;和&nbsp;<em style="background-color: inherit;">sendCustomCommand()</em>&nbsp;都会返回一个<em style="background-color: inherit;">FTPReply</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPReply</em>)对象.使用此对象，你可以检查服务器的响应代码和消息.&nbsp;<br /><em style="background-color: inherit;">FTPCodes&nbsp;</em>(<em style="background-color: inherit;">it.sauronsoftware.ftp4j.FTPCodes</em>) 接口报告了一些通用的FTP响应代码,因此你可以使用这些包中的某个来进行匹配.</p><h2><span style="font-size: 12pt; font-family: Arial;">异常处理</span></h2><p style="margin-right: 0px; margin-left: 0px;"><span style="font-size: 12pt; font-family: Arial;">ftp4j 包定义了五种类型的异常:</span></p><ul data-front-font-size="14px" style="margin-top: 0px; margin-bottom: 0px;"><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4.FTPException</em>)&nbsp;<br style="background-color: inherit;" />依赖于方法，这会报告抛出了一个FTP故障.你可以检查报告的错误码，使用<em style="background-color: inherit;">FTPCodes</em>&nbsp;常量来获取故障原因的详细信息.</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPIllegalReplyException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4.FTPIllegalReplyException</em>)<br style="background-color: inherit;" />这表示远程服务器使用非法方式进行了应答, 这与FTP是不兼容的. 这应该是非常罕见的.</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPListParseException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4.FTPListParseException</em>)<br style="background-color: inherit;" />这通常发生在<em style="background-color: inherit;">list()方法，如果服务器发回的响应不能被客户端包中现有解析器进行的话，就会抛出此种异常</em></li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPDataTransferException</em>&nbsp;(<em style="background-color: inherit;">it.sauronsoftware.ftp4.FTPDataTransferException</em>)<br style="background-color: inherit;" />当数据传输 (<em style="background-color: inherit;">download</em>,&nbsp;<em style="background-color: inherit;">upload</em>, but also&nbsp;<em style="background-color: inherit;">list</em>&nbsp;and&nbsp;<em style="background-color: inherit;">listNames</em>) 因网络连接错误失败时，就会抛出此种异常.</li><li style="background-color: inherit;"><em style="font-size: 12pt; font-family: Arial; background-color: inherit;">FTPAbortedException</em><span style="font-size: 12pt;">&nbsp;</span><span style="font-size: 12pt; background-color: inherit;">(</span><em style="font-size: 12pt; background-color: inherit;">it.sauronsoftware.ftp4.FTPAbortedException</em><span style="font-size: 12pt; background-color: inherit;">)</span><br style="font-size: 12pt; background-color: inherit;" /><span style="font-size: 12pt; background-color: inherit;">当数据传输 (</span><em style="font-size: 12pt; background-color: inherit;">download</em><span style="font-size: 12pt; background-color: inherit;">,</span><span style="font-size: 12pt;">&nbsp;</span><em style="font-size: 12pt; background-color: inherit;">upload</em><span style="font-size: 12pt; background-color: inherit;">, but also</span><span style="font-size: 12pt;">&nbsp;</span><em style="font-size: 12pt; background-color: inherit;">list</em><span style="font-size: 12pt;">&nbsp;</span><span style="font-size: 12pt; background-color: inherit;">and</span><span style="font-size: 12pt;">&nbsp;</span><em style="font-size: 12pt; background-color: inherit;">listNames</em><span style="font-size: 12pt; background-color: inherit;"><span style="background-color: inherit;"><span style="font-size: 16px; background-color: inherit;"><span style="font-size: 14px; background-color: inherit;">) 因客户端发出中断请求失败时，抛出的异常.</span></span></span></span></li></ul></div></div></div></div><img src ="http://www.blogjava.net/qbna350816/aggbug/430984.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qbna350816/" target="_blank">胡小军</a> 2016-06-21 22:34 <a href="http://www.blogjava.net/qbna350816/archive/2016/06/21/430984.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>