(commonsnet20api)英文版_第1页
(commonsnet20api)英文版_第2页
(commonsnet20api)英文版_第3页
(commonsnet20api)英文版_第4页
(commonsnet20api)英文版_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

OverviewPackageClassUseTreeDeprecatedIndexHelpPREV CLASS NEXT CLASSFRAMES NO FRAMES All Classes SUMMARY:NESTED|FIELD|CONSTR|METHODDETAIL:FIELD|CONSTR|METHOD.ftp Class FTPClientjava.lang.Object .SocketClient .ftp.FTP .ftp.FTPClientAll Implemented Interfaces: Configurable Direct Known Subclasses: FTPSClient public class FTPClientextends FTPimplements ConfigurableFTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from SocketClient, you must first connect to the server with connect before doing anything, and finally disconnect after youre completely finished interacting with the server. Then you need to check the FTP reply code to see if the connection was successful. For example: boolean error = false; try int reply; ftp.connect(); System.out.println(Connected to + server + .); System.out.print(ftp.getReplyString(); / After connection attempt, you should check the reply code to verify / success. reply = ftp.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply) ftp.disconnect(); System.err.println(FTP server refused connection.); System.exit(1); . / transfer files ftp.logout(); catch(IOException e) error = true; e.printStackTrace(); finally if(ftp.isConnected() try ftp.disconnect(); catch(IOException ioe) / do nothing System.exit(error ? 1 : 0); Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. 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. 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. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure. 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 . The only file types directly supported are FTP.ASCII_FILE_TYPE and FTP.BINARY_FILE_TYPE . Because there are at least 4 different EBCDIC encodings, we have opted not to provide direct support for 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. FTPClient uses the NetASCII filter streams to provide transparent handling of ASCII files. We will consider incorporating EBCDIC support if there is enough demand. FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE are the only supported formats, transfer modes, and file structures. Because the handling of sockets on different platforms can differ significantly, the FTPClient automatically issues a new PORT command prior to every transfer requiring that the server connect to the clients data port. This ensures identical problem-free behavior on Windows, Unix, and Macintosh platforms. Additionally, it relieves programmers from having to issue the PORT command themselves and dealing with platform dependent issues. 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. You may disable this behavior with 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). The FTPClient class will detect a premature FTP server connection closing when it receives a FTPReply.SERVICE_NOT_AVAILABLE response to a command. 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. When you encounter an FTPConnectionClosedException , you must disconnect the connection with disconnect() to properly clean up the system resources used by FTPClient. Before disconnecting, you may check the last reply code and text with getReplyCode , getReplyString , and getReplyStrings. You may avoid server disconnections while the client is idle by periodicaly 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. 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. Listing API Examples Both paged and unpaged examples of directory listings are available, as follows: Unpaged (whole list) access, using a parser accessible by auto-detect: FTPClient f=FTPClient(); f.connect(server); f.login(username, password); FTPFile files = listFiles(directory); Paged access, using a parser not accessible by auto-detect. The class defined in the first parameter of initateListParsing should be derived from .FTPFileEntryParser: FTPClient f=FTPClient(); f.connect(server); f.login(username, password); FTPListParseEngine engine = f.initiateListParsing(com.whatever.YourOwnParser, directory); while (engine.hasNext() FTPFile files = engine.getNext(25); / page size you want /do whatever you want with these files, display them, etc. /expensive FTPFile objects not created until needed. Paged access, using a parser accessible by auto-detect: FTPClient f=FTPClient(); f.connect(server); f.login(username, password); FTPListParseEngine engine = f.initiateListParsing(directory); while (engine.hasNext() FTPFile files = engine.getNext(25); / page size you want /do whatever you want with these files, display them, etc. /expensive FTPFile objects not created until needed. For examples of using FTPClient on servers whose directory listings use languages other than English use date formats other than the American English standard MM d yyyy are in different timezones and you need accurate timestamps for dependency checking as in Ant see FTPClientConfig. Author: Daniel F. Savarese, Rory Winston See Also: FTP, FTPConnectionClosedException, FTPFileEntryParser, FTPFileEntryParserFactory, DefaultFTPFileEntryParserFactory, FTPClientConfig, MalformedServerReplyExceptionField SummarystaticintACTIVE_LOCAL_DATA_CONNECTION_MODE A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server should connect to the clients data port to initiate a data transfer.staticintACTIVE_REMOTE_DATA_CONNECTION_MODE A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to should connect to the other servers data port to initiate a data transfer.staticintPASSIVE_LOCAL_DATA_CONNECTION_MODE A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server is in passive mode, requiring the client to connect to the servers data port to initiate a transfer.staticintPASSIVE_REMOTE_DATA_CONNECTION_MODE A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to is in passive mode, requiring the other server to connect to the first servers data port to initiate a data transfer. Fields inherited from class .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 .SocketClient_defaultPort_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL Constructor SummaryFTPClient() Default FTPClient constructor. Method Summaryprotected void_connectAction_() Initiates control connections and gets initial tected Socket_openDataConnection_(intcommand, Stringarg) Establishes a data connection with the FTP server, returning a Socket for the connection if successful.booleanabort() Abort a transfer in progress.booleanallocate(intbytes) Reserve a number of bytes on the server for the next file transfer.booleanallocate(intbytes, intrecordSize) Reserve space on the server for the next file transfer.booleanappendFile(Stringremote, InputStreamlocal) Appends to a file on the server with the given name, taking input from the given InputStream.OutputStreamappendFileStream(Stringremote) Returns an OutputStream through which data can be written to append to a file on the server with the given name.booleanchangeToParentDirectory() Change to the parent directory of the current working directory.booleanchangeWorkingDirectory(Stringpathname) Change the current working directory of the FTP session.booleancompletePendingCommand() There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction.voidconfigure(FTPClientConfigconfig) Implementation of the Configurable interface.booleandeleteFile(Stringpathname) Deletes a file on the FTP server.voiddisconnect() Closes the connection to the FTP server and restores connection parameters to the default values.voidenterLocalActiveMode() Set the current data connection mode to ACTIVE_LOCAL_DATA_CONNECTION_MODE.voidenterLocalPassiveMode() Set the current data connection mode to PASSIVE_LOCAL_DATA_CONNECTION_MODE .booleanenterRemoteActiveMode(InetAddresshost, intport) Set the current data connection mode to ACTIVE_REMOTE_DATA_CONNECTION .booleanenterRemotePassiveMode() Set the current data connection mode to PASSIVE_REMOTE_DATA_CONNECTION_MODE .intgetBufferSize() Retrieve the current internal buffer getDataConnectionMode() Returns the current data connection mode (one of the _DATA_CONNECTION_MODE tected StringgetListArguments(Stringpathname) booleangetListHiddenFiles() StringgetModificationTime(Stringpathname) Issue the FTP MDTM command (not supported by all servers to retrieve the last modification time of a file.StringgetPassiveHost() Returns the hostname or IP address (in the form of a string) returned by the server when entering passive getPassivePort() If in passive mode, returns the data port of the passive host.longgetRestartOffset() Fetches the restart offset.StringgetStatus() Issue the FTP STAT command to the server.StringgetStatus(Stringpathname) Issue the FTP STAT command to the server for a given pathname.StringgetSystemName() Fetches the system type name from the server and returns the string.FTPListParseEngineinitiateListParsing() Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the current working directory on the server This information is obtained through the LIST command.FTPListParseEngineinitiateListParsing(Stringpathname) Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the supplied directory.FTPListParseEngineinitiateListParsing(StringparserKey, Stringpathname) Using the supplied parser key, initialize an FTPListParseEngine object containing a raw file information for the supplied directory.booleanisRemoteVerificationEnabled() Return whether or not verification of the remote host participating in data connections is enabled.FTPFilelistFiles() Using the default system autodetect mechanism, obtain a list of file information for the current working directory.FTPFilelistFiles(Stringpathname) Using the default system autodetect mechanism, obtain a list of file information for the current working directory or for just a single file.StringlistHelp() Fetches the system help information from the server and returns the full string.StringlistHelp(Stringcommand) Fetches the help information for a given command from the server and returns the full string.StringlistNames() Obtain a list of filenames in the current working directory This information is obtained through the NLST command.StringlistNames(Stringpathname) Obtain a list of filenames in a directory (or just the name of a given file, which is not particularly useful).booleanlogin(Stringusername, Stringpassword) Login to the FTP server using the provided username and password.booleanlogin(Stringusername, Stringpassword, Stringaccount) Login to the FTP server using the provided username, password, and account.booleanlogout() Logout of the FTP server by sending the QUIT command.booleanmakeDirectory(Stringpathname) Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).StringprintWorkingDirectory() Returns the pathname of the current working directory.booleanremoteAppend(Stringfilename) Initiate a server to server file transfer.booleanremoteRetrieve(Stringfilename) Initiate a server to server file transfer.booleanremoteStore(Stringfilename) Initiate a server to server file transfer.booleanremoteStoreUnique() Initiate a server to server file transfer.booleanremoteStoreUnique(Stringfilename) Initiate a server to server file transfer.booleanremoveDirectory(Stringpathname) Removes a directory on the FTP server (if empty).booleanrename(Stringfrom, Stringto) Renames a remote file.booleanretrieveFile(Stringremote, OutputStreamlocal) Retrieves a named file from the server and writes it to the given OutputStream.InputStreamretrieveFileStream(Stringremote) Returns an InputStream from which a named file from the server can be read.booleansendNoOp() Sends a NOOP command to the FTP server.booleansendSiteCommand(Stringarguments) Send a site specific command.voidsetBufferSize(intbufSize) Set the internal buffer size.voidsetDataTimeout(inttimeout) Sets the timeout in milliseconds to use when reading from the data connection.booleansetFileStructure(intstructure) Sets the file structure.booleansetFileTransferMode(intmode) Sets the transfer mode.booleansetFileType(intfileType) Sets the file type to be transferred.booleansetFileType(intfileType, intformatOrByteSize) Sets the file type to be transferred and the format.voidsetListHiddenFiles(booleanlistHiddenFiles) You can set this to true if you would like to get hidden files when listFiles(java.lang.String) too.voidsetParserFactory(FTPFileEntryParserFactoryparserFactory) set the factory used for parser creation to the supplied factory object.voidsetRemoteVerificationEnabled(booleanenable) Enable or disable verification that the remote host taking part of a data connection is the same as the host to which the control connection is attached.voidsetRestartOffset(longoffset) Sets the restart offset.booleanstoreFile(Stringremote, InputStreamlocal) Stores a file on the server using the given name and taking input from the given InputStream.OutputStreamstoreFileStream(Stringremote) Returns an OutputStream through which data can be written to store a file on the server using the given name.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论