ftp.java
来自「apache推出的net包」· Java 代码 · 共 1,475 行 · 第 1/5 页
JAVA
1,475 行
/* * Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.net.ftp;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.InetAddress;import java.net.Socket;import java.net.SocketException;import java.util.Enumeration;import java.util.Vector;import org.apache.commons.net.MalformedServerReplyException;import org.apache.commons.net.ProtocolCommandListener;import org.apache.commons.net.ProtocolCommandSupport;import org.apache.commons.net.SocketClient;import org.apache.commons.net.telnet.TelnetClient;/*** * FTP provides the basic the functionality necessary to implement your * own FTP client. It extends org.apache.commons.net.TelnetClient * simply because it saves the writing of extra code to handle the FTP * control connection which always remains open during an FTP session and * uses the Telnet protocol. Aggregation would require writing new * wrapper methods and wouldn't leverage the functionality already * present in org.apache.commons.net.SocketClient. * <p> * To derive the full benefits of the FTP class requires some knowledge * of the FTP protocol defined in RFC 959. However, there is no reason * why you should have to use the FTP class. The * {@link org.apache.commons.net.ftp.FTPClient} class, * derived from FTP, * implements all the functionality required of an FTP client. The * FTP class is made public to provide access to various FTP constants * and to make it easier for adventurous programmers (or those with * special needs) to interact with the FTP protocol and implement their * own clients. A set of methods with names corresponding to the FTP * command names are provided to facilitate this interaction. * <p> * 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 FTP class will detect a * premature FTP server connection closing when it receives a * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE } * response to a command. * When that occurs, the FTP class method encountering that reply will throw * an {@link org.apache.commons.net.ftp.FTPConnectionClosedException} * . <code>FTPConectionClosedException</code> * is a subclass of <code> IOException </code> 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 <code> IOException </code> * catch block. When you encounter an * {@link org.apache.commons.net.ftp.FTPConnectionClosedException} * , you must disconnect the connection with * {@link #disconnect disconnect() } to properly clean up the * system resources used by FTP. Before disconnecting, you may check the * last reply code and text with * {@link #getReplyCode getReplyCode }, * {@link #getReplyString getReplyString }, * and {@link #getReplyStrings getReplyStrings}. * You may avoid server disconnections while the client is idle by * periodicaly sending NOOP commands to the server. * <p> * 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 * {@link org.apache.commons.net.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. * <p> * <p> * @author Daniel F. Savarese * @see FTPClient * @see FTPConnectionClosedException * @see org.apache.commons.net.MalformedServerReplyException ***/public class FTP extends TelnetClient{ /*** The default FTP data port (20). ***/ public static final int DEFAULT_DATA_PORT = 20; /*** The default FTP control port (21). ***/ public static final int DEFAULT_PORT = 21; /*** * A constant used to indicate the file(s) being transfered should * be treated as ASCII. This is the default file type. All constants * ending in <code>FILE_TYPE</code> are used to indicate file types. ***/ public static final int ASCII_FILE_TYPE = 0; /*** * A constant used to indicate the file(s) being transfered should * be treated as EBCDIC. Note however that there are several different * EBCDIC formats. All constants ending in <code>FILE_TYPE</code> * are used to indicate file types. ***/ public static final int EBCDIC_FILE_TYPE = 1; /*** * A constant used to indicate the file(s) being transfered should * be treated as a binary image, i.e., no translations should be * performed. All constants ending in <code>FILE_TYPE</code> are used to * indicate file types. ***/ public static final int IMAGE_FILE_TYPE = 2; /*** * A constant used to indicate the file(s) being transfered should * be treated as a binary image, i.e., no translations should be * performed. All constants ending in <code>FILE_TYPE</code> are used to * indicate file types. ***/ public static final int BINARY_FILE_TYPE = 2; /*** * A constant used to indicate the file(s) being transfered should * be treated as a local type. All constants ending in * <code>FILE_TYPE</code> are used to indicate file types. ***/ public static final int LOCAL_FILE_TYPE = 3; /*** * A constant used for text files to indicate a non-print text format. * This is the default format. * All constants ending in <code>TEXT_FORMAT</code> are used to indicate * text formatting for text transfers (both ASCII and EBCDIC). ***/ public static final int NON_PRINT_TEXT_FORMAT = 4; /*** * A constant used to indicate a text file contains format vertical format * control characters. * All constants ending in <code>TEXT_FORMAT</code> are used to indicate * text formatting for text transfers (both ASCII and EBCDIC). ***/ public static final int TELNET_TEXT_FORMAT = 5; /*** * A constant used to indicate a text file contains ASA vertical format * control characters. * All constants ending in <code>TEXT_FORMAT</code> are used to indicate * text formatting for text transfers (both ASCII and EBCDIC). ***/ public static final int CARRIAGE_CONTROL_TEXT_FORMAT = 6; /*** * A constant used to indicate a file is to be treated as a continuous * sequence of bytes. This is the default structure. All constants ending * in <code>_STRUCTURE</code> are used to indicate file structure for * file transfers. ***/ public static final int FILE_STRUCTURE = 7; /*** * A constant used to indicate a file is to be treated as a sequence * of records. All constants ending in <code>_STRUCTURE</code> * are used to indicate file structure for file transfers. ***/ public static final int RECORD_STRUCTURE = 8; /*** * A constant used to indicate a file is to be treated as a set of * independent indexed pages. All constants ending in * <code>_STRUCTURE</code> are used to indicate file structure for file * transfers. ***/ public static final int PAGE_STRUCTURE = 9; /*** * A constant used to indicate a file is to be transfered as a stream * of bytes. This is the default transfer mode. All constants ending * in <code>TRANSFER_MODE</code> are used to indicate file transfer * modes. ***/ public static final int STREAM_TRANSFER_MODE = 10; /*** * A constant used to indicate a file is to be transfered as a series * of blocks. All constants ending in <code>TRANSFER_MODE</code> are used * to indicate file transfer modes. ***/ public static final int BLOCK_TRANSFER_MODE = 11; /*** * A constant used to indicate a file is to be transfered as FTP * compressed data. All constants ending in <code>TRANSFER_MODE</code> * are used to indicate file transfer modes. ***/ public static final int COMPRESSED_TRANSFER_MODE = 12; // We have to ensure that the protocol communication is in ASCII // but we use ISO-8859-1 just in case 8-bit characters cross // the wire. /** * The default character encoding used for communicating over an * FTP control connection. The default encoding is an * ASCII-compatible encoding. Some FTP servers expect other * encodings. You can change the encoding used by an FTP instance * with {@link #setControlEncoding setControlEncoding}. */ public static final String DEFAULT_CONTROL_ENCODING = "ISO-8859-1"; private static final String __modes = "ABILNTCFRPSBC"; private StringBuffer __commandBuffer; BufferedReader _controlInput; BufferedWriter _controlOutput; int _replyCode; Vector _replyLines; boolean _newReplyString; String _replyString; String _controlEncoding; /*** * A ProtocolCommandSupport object used to manage the registering of * ProtocolCommandListeners and te firing of ProtocolCommandEvents. ***/ protected ProtocolCommandSupport _commandSupport_; /*** * The default FTP constructor. Sets the default port to * <code>DEFAULT_PORT</code> and initializes internal data structures * for saving FTP reply information. ***/ public FTP() { setDefaultPort(DEFAULT_PORT); __commandBuffer = new StringBuffer(); _replyLines = new Vector(); _newReplyString = false; _replyString = null; _commandSupport_ = new ProtocolCommandSupport(this); _controlEncoding = DEFAULT_CONTROL_ENCODING; } private void __getReply() throws IOException { int length; _newReplyString = true; _replyLines.setSize(0); String line = _controlInput.readLine(); if (line == null) throw new FTPConnectionClosedException( "Connection closed without indication."); // In case we run into an anomaly we don't want fatal index exceptions // to be thrown. length = line.length(); if (length < 3) throw new MalformedServerReplyException( "Truncated server reply: " + line); try { String code = line.substring(0, 3); _replyCode = Integer.parseInt(code); } catch (NumberFormatException e) { throw new MalformedServerReplyException( "Could not parse response code.\nServer Reply: " + line); } _replyLines.addElement(line); // Get extra lines if message continues. if (length > 3 && line.charAt(3) == '-') { do { line = _controlInput.readLine();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?