org.apache.commons.NET.ftp
Class FTPClient類FTPClient
java.lang.Object java.lang.Object繼承
org.apache.commons.net.SocketClient org.apache.commons.net.SocketClient
org.apache.commons.net.ftp.FTP org.apache.commons.net.ftp.FTP
org.apache.commons.net.ftp.FTPClient org.apache.commons.Net.ftp.FTPClient
All Implemented Interfaces: 所有已實現的接口:
Direct Known Subclasses: 直接已知子類:
FTPHTTPClient , FTPSClient FTPHTTPClient , FTPSClient
public class FTPClient公共類FTPClient
implements Configurable實現了可配置
FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. FTPClient封裝了所有必要的功能來存儲和檢索從FTP服務器上的文件。 This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface.這個類負責所有與FTP服務器交互的底層細節,並提供了便捷的更高層次的接口。 As with all classes derived from SocketClient , you must first connect to the server with connect before doing anything, and finally disconnect after you're completely finished interacting with the server.正如來自所有類SocketClient ,您必須首先連接到與服務器connect做任何事之前,最后disconnect完成后,你完全與服務器交互。 Then you need to check the FTP reply code to see if the connection was successful.然后,你需要檢查的FTP答復代碼,看看是否連接成功。 For example:例如:
boolean error = false;布爾錯誤= 0;
try {嘗試{
int reply;詮釋答復;
ftp.connect("ftp.foobar.com"); ftp.connect(“ftp.foobar.com”);
System.out.println("Connected to " + server + "."); System.out.println(“連接到”+服務器+ ".");
System.out.print(ftp.getReplyString()); System.out.print(ftp.getReplyString());
// After connection attempt, you should check the reply code to verify / /連接嘗試后,你應該檢查代碼以驗證答復
// success. / /成功。
reply = ftp.getReplyCode();答復= ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {如果(!FTPReply.isPositiveCompletion(回復)){
ftp.disconnect(); ftp.disconnect();
System.err.println("FTP server refused connection."); System.err.println(“FTP服務器拒絕連接。”);
System.exit(1); System.exit(1);
} }
... ... // transfer files / /傳送文件
ftp.logout(); ftp.logout();
} catch(IOException e) { }捕捉(IOException異常五){
error = true;誤差為真;
e.printStackTrace(); e.printStackTrace();
} finally {最后} {
if(ftp.isConnected()) {如果(ftp.isConnected()){
try {嘗試{
ftp.disconnect(); ftp.disconnect();
} catch(IOException ioe) { }捕捉(IOException異常雇主組織){
// do nothing / /什么也不做
} }
} }
System.exit(error ? 1 : 0); System.exit(錯誤1:0?);
} }
Immediately after connecting is the only real time you need to check the reply code (because connect is of type void).連接后立即是唯一真正的時候你需要檢查答復代碼(因為是連接類型為void)。 The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value.對於所有的FTP FTPClient指揮方法的公約就是這樣,他們要么返回一個布爾值或其他值。 The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure.該方法返回一個布爾從FTP服務器成功完成答辯,假假真真的錯誤條件中的一個或故障而導致的答復。 The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure.該方法返回一個布爾值返回值比含有較高水平的FTP命令,或者為null,如果產生一個錯誤條件答復或故障導致其他數據。 If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.如果您要訪問的FTP的確切答復代碼導致成功或失敗,你必須調用getReplyCode后,成功或失敗。
The default settings for FTPClient are for it to use FTP.ASCII_FILE_TYPE , FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE . FTPClient的默認設置是它使用FTP.ASCII_FILE_TYPE , FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE和FTP.FILE_STRUCTURE 。 The only file types directly supported are FTP.ASCII_FILE_TYPE and FTP.BINARY_FILE_TYPE .唯一的文件直接支持的類型是FTP.ASCII_FILE_TYPE和FTP.BINARY_FILE_TYPE 。 Because there are at least 4 different EBCDIC encodings, we have opted not to provide direct support for EBCDIC.因為至少有4種不同的EBCDIC編碼,我們還選擇了不提供直接支持的EBCDIC。 To transfer EBCDIC and other unsupported file types you must create your own filter InputStreams and OutputStreams and wrap them around the streams returned or required by the FTPClient methods.為了轉移EBCDIC和其他不支持的文件類型,你必須創建自己的過濾InputStreams和OutputStreams和總結他們周圍的流退回或由FTPClient方法所需。 FTPClient uses the NetASCII filter streams to provide transparent handling of ASCII files. FTPClient使用NetASCII過濾器流提供ASCII文件透明處理。 We will consider incorporating EBCDIC support if there is enough demand.我們會考慮把EBCDIC碼的支持,如果有足夠的需求。
FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE are the only supported formats, transfer modes, and file structures. FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE和FTP.FILE_STRUCTURE是唯一支持的格式,傳輸模式和文件結構。
Because the handling of sockets on different platforms can differ significantly, the FTPClient automatically issues a new PORT (or EPRT) command prior to every transfer requiring that the server connect to the client's data port.由於在不同平台上插座處理可以顯着差異,在FTPClient自動發出一個新的端口(或EPRT)命令之前,每次傳輸要求的服務器連接到客戶端的數據端口。 This ensures identical problem-free behavior on Windows, Unix, and Macintosh platforms.這將確保相同的Windows,Unix和Macintosh平台無故障的行為。 Additionally, it relieves programmers from having to issue the PORT (or EPRT) command themselves and dealing with platform dependent issues.此外,它解除不必發出端口(或EPRT)命令自己和與平台有關問題的程序員。
Additionally, for security purposes, all data connections to the client are verified to ensure that they originated from the intended party (host and port).此外,為了安全起見,所有的數據連接到客戶端進行驗證,以確保他們打算從黨(主機和端口)起源。 If a data connection is initiated by an unexpected party, the command will close the socket and throw an IOException.如果一個數據連接是由黨發起的一個意外,該命令將關閉套接字並拋出一個IOException異常。 You may disable this behavior with setRemoteVerificationEnabled() .您可以禁用此行為setRemoteVerificationEnabled()
You should keep in mind that the FTP server may choose to prematurely close a connection if the client has been idle for longer than a given time period (usually 900 seconds).你應該記住,在FTP服務器可以選擇過早關閉連接,如果客戶已超過給定的時間較長時期(通常為900秒)閑置。 The FTPClient class will detect a premature FTP server connection closing when it receives a FTPReply.SERVICE_NOT_AVAILABLE response to a command.將檢測的FTPClient類FTP服務器連接過早關閉,當它收到FTPReply.SERVICE_NOT_AVAILABLE響應命令。 When that occurs, the FTP class method encountering that reply will throw an FTPConnectionClosedException . FTPConnectionClosedException is a subclass of IOException and therefore need not be caught separately, but if you are going to catch it separately, its catch block must appear before the more general IOException catch block.一旦這種情況發生時,FTP類方法遇到的答復將拋出一個FTPConnectionClosedException 。 FTPConnectionClosedException是一個子類IOException ,因此不必分別被捕獲,但如果你要抓住它分開,它的catch塊必須出現在更一般的IOException catch塊。 When you encounter an FTPConnectionClosedException , you must disconnect the connection with disconnect() to properly clean up the system resources used by FTPClient.當你遇到一個FTPConnectionClosedException ,必須斷開與連接disconnect()妥善清理,系統資源使用FTPClient。 Before disconnecting, you may check the last reply code and text with getReplyCode , getReplyString , and getReplyStrings .在斷開,你可以檢查代碼和文本的最后答復與getReplyCode , getReplyString和getReplyStrings 。 You may avoid server disconnections while the client is idle by periodically sending NOOP commands to the server.您可能會避免服務器斷開,而客戶端閑置的空操作指令通過定期發送到服務器。
Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a MalformedServerReplyException , which is a subclass of IOException.它不是單獨列出每個方法,我們在這里提到的每個方法與服務器通信,並拋出一個IOException異常也可引發MalformedServerReplyException ,這是一個IOException異常子類。 A MalformedServerReplyException will be thrown when the reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as lenient as possible.將拋出一個MalformedServerReplyException答復時,從服務器收到的偏離足夠的協議規范,它不能在一個有用的方式解釋盡管試圖盡可能寬松。
Listing API Examples Both paged and unpaged examples of directory listings are available, as follows:上市API的兩個例子的目錄列表分頁和無頁數的范例,如下:
Unpaged (whole list) access, using a parser accessible by auto-detect:無頁數(全名單)訪問,使用分析器可經自動檢測:
FTPClient f = new FTPClient(); FTPClient f =new FTPClient();
f.connect(server); f.connect(服務器);
f.login(username, password); f.login(用戶名,密碼);
FTPFile[] files = listFiles(directory); FTPFile []文件= listfiles <套件(目錄);
Paged access, using a parser not accessible by auto-detect.分頁訪問,使用分析器無法訪問的自動檢測。 The class defined in the first parameter of initateListParsing should be derived from org.apache.commons.net.FTPFileEntryParser:在應該從org.apache.commons.net.FTPFileEntryParser派生initateListParsing第一個參數定義的類:
FTPClient f = new FTPClient(); FTPClient f =new FTPClient();
f.connect(server); f.connect(服務器);
f.login(username, password); f.login(用戶名,密碼);
FTPListParseEngine engine = FTPListParseEngine引擎=
f.initiateListParsing("com.whatever.YourOwnParser", directory); f.initiateListParsing(“com.whatever.YourOwnParser”,目錄);
while (engine.hasNext()) {而(engine.hasNext()){
FTPFile[] files = engine.getNext(25); // "page size" you want FTPFile []文件= engine.getNext(25); / /“頁面大小”你想要
//do whatever you want with these files, display them, etc. / /做你想做這些文件,顯示它們,等
//expensive FTPFile objects not created until needed. / /昂貴FTPFile不創建對象,直到需要。
} }
Paged access, using a parser accessible by auto-detect:分頁訪問,使用分析器可經自動檢測:
FTPClient f = new FTPClient(); FTPClient f =new FTPClient();
f.connect(server); f.connect(服務器);
f.login(username, password); f.login(用戶名,密碼);
FTPListParseEngine engine = f.initiateListParsing(directory); FTPListParseEngine engine = f.initiateListParsing(目錄);
while (engine.hasNext()) {而(engine.hasNext()){
FTPFile[] files = engine.getNext(25); // "page size" you want FTPFile []文件= engine.getNext(25); / /“頁面大小”你想要
//do whatever you want with these files, display them, etc. / /做你想做這些文件,顯示它們,等
//expensive FTPFile objects not created until needed. / /昂貴FTPFile不創建對象,直到需要。
} }
For examples of using FTPClient on servers whose directory listings有關使用服務器上的目錄清單FTPClient例子
· use languages other than English使用英語以外的語言
· use date formats other than the American English "standard" MM d yyyy使用日期格式“以外的美國英語”標准MM d yyyy
· are in different timezones and you need accurate timestamps for dependency checking as in Ant在不同的時區,你需要在蟻群依賴檢查准確的時間戳
see FTPClientConfig .見FTPClientConfig 。
Author: 作者:
Daniel F. Savarese, Rory Winston丹尼爾樓Savarese,羅里溫斯頓
See Also: 另見:
FTP , FTPConnectionClosedException , FTPFileEntryParser , FTPFileEntryParserFactory , DefaultFTPFileEntryParserFactory , FTPClientConfig , MalformedServerReplyException FTP , FTPConnectionClosedException , FTPFileEntryParser , FTPFileEntryParserFactory , DefaultFTPFileEntryParserFactory , FTPClientConfig , MalformedServerReplyException
Field Summary 字段摘要 |
|
static int |
ACTIVE_LOCAL_DATA_CONNECTION_MODE |
static int |
ACTIVE_REMOTE_DATA_CONNECTION_MODE |
static int |
PASSIVE_LOCAL_DATA_CONNECTION_MODE |
static int |
PASSIVE_REMOTE_DATA_CONNECTION_MODE |
Fields inherited from class org.apache.commons.net.ftp. FTP org.apache.commons.net.ftp類從繼承的字段。 的FTP |
_commandSupport_ , _controlEncoding , _controlInput_ , _controlOutput_ , _newReplyString , _replyCode , _replyLines , _replyString , ASCII_FILE_TYPE , BINARY_FILE_TYPE , BLOCK_TRANSFER_MODE , CARRIAGE_CONTROL_TEXT_FORMAT , COMPRESSED_TRANSFER_MODE , DEFAULT_CONTROL_ENCODING , DEFAULT_DATA_PORT , DEFAULT_PORT , EBCDIC_FILE_TYPE , FILE_STRUCTURE , LOCAL_FILE_TYPE , NON_PRINT_TEXT_FORMAT , PAGE_STRUCTURE , RECORD_STRUCTURE , STREAM_TRANSFER_MODE , strictMultilineParsing , TELNET_TEXT_FORMAT |
Fields inherited from class org.apache.commons.net. SocketClient org.apache.commons.net類從繼承的字段。 SocketClient |
_defaultPort_ , _input_ , _output_ , _serverSocketFactory_ , _socket_ , _socketFactory_ , _timeout_ , connectTimeout , NETASCII_EOL |
Constructor Summary 構造方法摘要 |
|
FTPClient () |
Method Summary 方法摘要 |
|
protected void |
_connectAction_ () |
protected Socket |
_openDataConnection_ (int command, String arg) |
boolean |
abort () |
boolean |
allocate (int bytes) |
boolean |
allocate (int bytes, int recordSize) |
boolean |
appendFile ( String remote, InputStream local) |
appendFileStream ( String remote) |
|
boolean |
changeToParentDirectory () |
boolean |
changeWorkingDirectory ( String pathname) |
boolean |
completePendingCommand () |
void |
configure ( FTPClientConfig config) |
boolean |
deleteFile ( String pathname) |
void |
disconnect () |
void |
enterLocalActiveMode () |
void |
enterLocalPassiveMode () |
boolean |
enterRemoteActiveMode ( InetAddress host, int port) |
boolean |
enterRemotePassiveMode () |
boolean |
features () |
int |
getBufferSize () |
int |
getDataConnectionMode () |
protected String |
getListArguments ( String pathname) |
boolean |
|
getModificationTime ( String pathname) |
|
getPassiveHost () |
|
int |
getPassivePort () |
long |
getRestartOffset () |
getStatus () |
|
getStatus ( String pathname) |
|
getSystemName () |
|
getSystemType () |
|
initiateListParsing () |
|
initiateListParsing ( String pathname) |
|
initiateListParsing ( String parserKey, String pathname) |
|
boolean |
isRemoteVerificationEnabled () |
boolean |
isUseEPSVwithIPv4 () |
FTPFile [] |
listFiles () |
FTPFile [] |
listFiles ( String pathname) |
FTPFile [] |
listFiles ( String pathname, FTPFileFilter filter) |
listHelp () |
|
listHelp ( String command) |
|
String [] |
listNames () |
String [] |
listNames ( String pathname) |
boolean |
login ( String username, String password) |
boolean |
login ( String username, String password, String account) |
boolean |
logout () |
boolean |
makeDirectory ( String pathname) |
printWorkingDirectory () |
|
boolean |
remoteAppend ( String filename) |
boolean |
remoteRetrieve ( String filename) |
boolean |
remoteStore ( String filename) |
boolean |
remoteStoreUnique () |
boolean |
remoteStoreUnique ( String filename) |
boolean |
removeDirectory ( String pathname) |
boolean |
rename ( String from, String to) |
boolean |
retrieveFile ( String remote, OutputStream local) |
retrieveFileStream ( String remote) |
|
boolean |
sendNoOp () |
boolean |
sendSiteCommand ( String arguments) |
void |
setActiveExternalIPAddress ( String ipAddress) |
void |
setActivePortRange (int minPort, int maxPort) |
void |
setBufferSize (int bufSize) |
void |
setDataTimeout (int timeout) |
boolean |
setFileStructure (int structure) |
boolean |
setFileTransferMode (int mode) |
boolean |
setFileType (int fileType) |
boolean |
setFileType (int fileType, int formatOrByteSize) |
void |
setListHiddenFiles (boolean listHiddenFiles) |
boolean |
setModificationTime ( String pathname, String timeval) |
void |
setParserFactory ( FTPFileEntryParserFactory parserFactory) |
void |
setRemoteVerificationEnabled (boolean enable) |
void |
setRestartOffset (long offset) |
void |
setUseEPSVwithIPv4 (boolean selected) |
boolean |
storeFile ( String remote, InputStream local) |
storeFileStream ( String remote) |
|
boolean |
storeUniqueFile ( InputStream local) |
boolean |
storeUniqueFile ( String remote, InputStream local) |
storeUniqueFileStream () |
|
storeUniqueFileStream ( String remote) |
|
boolean |
structureMount ( String pathname) |
Methods inherited from class org.apache.commons.net.ftp. FTP 從類繼承的方法org.apache.commons.net.ftp。 FTP的 |
abor , acct , addProtocolCommandListener , allo , allo , appe , cdup , cwd , dele , eprt , epsv , feat , getControlEncoding , getReply , getReplyCode , getReplyString , getReplyStrings , help , help , isStrictMultilineParsing , list , list , mdtm , mfmt , mkd , mode , nlst , nlst , noop , pass , pasv , port , pwd , quit , rein , removeProtocolCommandListener , rest , retr , rmd , rnfr , rnto , sendCommand , sendCommand , sendCommand , sendCommand , setControlEncoding , setStrictMultilineParsing , site , smnt , stat , stat , stor , stou , stou , stru , syst , type , type , user |
Methods inherited from class org.apache.commons.net. SocketClient 從類繼承的方法org.apache.commons.net。 SocketClient |
connect , connect , connect , connect , connect , connect , getConnectTimeout , getDefaultPort , getDefaultTimeout , getKeepAlive , getLocalAddress , getLocalPort , getRemoteAddress , getRemotePort , getServerSocketFactory , getSoLinger , getSoTimeout , getTcpNoDelay , isConnected , setConnectTimeout , setDefaultPort , setDefaultTimeout , setKeepAlive , setReceiveBufferSize , setSendBufferSize , setServerSocketFactory , setSocketFactory , setSoLinger , setSoTimeout , setTcpNoDelay , verifyRemote |
Methods inherited from class Java.lang. Object java.lang中的類繼承的方法。 對象 |
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |