/*
* ftp4j - A pure Java FTP client library
*
* Copyright (C) 2008-2010 Carlo Pelliccia (www.sauronsoftware.it)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
package it.sauronsoftware.ftp4j;
import it.sauronsoftware.ftp4j.connectors.DirectConnector;
import it.sauronsoftware.ftp4j.extrecognizers.DefaultTextualExtensionRecognizer;
import it.sauronsoftware.ftp4j.extrecognizers.ParametricTextualExtensionRecognizer;
import it.sauronsoftware.ftp4j.listparsers.DOSListParser;
import it.sauronsoftware.ftp4j.listparsers.EPLFListParser;
import it.sauronsoftware.ftp4j.listparsers.MLSDListParser;
import it.sauronsoftware.ftp4j.listparsers.NetWareListParser;
import it.sauronsoftware.ftp4j.listparsers.UnixListParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import javax.net.ssl.SSLSocketFactory;
/**
* This class implements a FTP client.
*
* You can use an instance of this class to connect to a remote FTP site and do
* FTP operations like directory listing, file upload and download, resume a
* broken upload/download and so on.
*
* The common flow is: create the object, connect to a remote FTP site with the
* connect() method, authenticate with login(), do anything you need with the
* contents of the remote site, quit the site with disconnect().
*
* A FTPClient object can handle a connection per time. Once you have used and
* disconnected a FTPClient object you can use it again to connect another FTP
* server.
*
* @author Carlo Pelliccia
* @version 1.5
*/
public class FTPClient {
/**
* The constant for the FTP security level.
*
* @since 1.4
*/
public static final int SECURITY_FTP = 0;
/**
* The constant for the FTPS (FTP over implicit TLS/SSL) security level.
*
* @since 1.4
*/
public static final int SECURITY_FTPS = 1;
/**
* The constant for the FTPES (FTP over explicit TLS/SSL) security level.
*
* @since 1.4
*/
public static final int SECURITY_FTPES = 2;
/**
* The constant for the AUTO file transfer type. It lets the client pick
* between textual and binary types, depending on the extension of the file
* exchanged through a textual extension recognizer.
*/
public static final int TYPE_AUTO = 0;
/**
* The constant for the TEXTUAL file transfer type. It means that the data
* sent or received is treated as textual information. This implies charset
* conversion during the transfer.
*/
public static final int TYPE_TEXTUAL = 1;
/**
* The constant for the BINARY file transfer type. It means that the data
* sent or received is treated as a binary stream. The data is taken "as
* is", without any charset conversion.
*/
public static final int TYPE_BINARY = 2;
/**
* The constant for the MLSD policy that causes the client to use the MLSD
* command instead of LIST, but only if the MLSD command is explicitly
* supported by the server (the support is tested with the FEAT command).
*
* @since 1.5
*/
public static final int MLSD_IF_SUPPORTED = 0;
/**
* The constant for the MLSD policy that causes the client to use always the
* MLSD command instead of LIST, also if the MLSD command is not explicitly
* supported by the server (the support is tested with the FEAT command).
*
* @since 1.5
*/
public static final int MLSD_ALWAYS = 1;
/**
* The constant for the MLSD policy that causes the client to use always the
* LIST command, also if the MLSD command is explicitly supported by the
* server (the support is tested with the FEAT command).
*
* @since 1.5
*/
public static final int MLSD_NEVER = 2;
/**
* The size of the buffer used when sending or receiving data.
*
* @since 1.6
*/
private static final int SEND_AND_RECEIVE_BUFFER_SIZE = 64 * 1024;
/**
* The DateFormat object used to parse the reply to a MDTM command.
*/
private static final DateFormat MDTM_DATE_FORMAT = new SimpleDateFormat(
"yyyyMMddHHmmss");
/**
* The RegExp Pattern object used to parse the reply to a PASV command.
*/
private static final Pattern PASV_PATTERN = Pattern
.compile("\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}");
/**
* The RegExp Pattern object used to parse the reply to a PWD command.
*/
private static final Pattern PWD_PATTERN = Pattern.compile("\"/.*\"");
/**
* The connector used to connect the remote host.
*/
private FTPConnector connector = new DirectConnector();
/**
* The SSL socket factory used to negotiate SSL connections.
*/
private SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
/**
* The FTPCommunicationListener objects registered on the client.
*/
private ArrayList communicationListeners = new ArrayList();
/**
* The FTPListParser objects registered on the client.
*/
private ArrayList listParsers = new ArrayList();
/**
* The textual extension recognizer used by the client.
*/
private FTPTextualExtensionRecognizer textualExtensionRecognizer = DefaultTextualExtensionRecognizer.getInstance();
/**
* The FTPListParser used successfully during previous connection-scope list
* operations.
*/
private FTPListParser parser = null;
/**
* If the client is connected, it reports the remote host name or address.
*/
private String host = null;
/**
* If the client is connected, it reports the remote port number.
*/
private int port = 0;
/**
* The security level. The value should be one of SECURITY_FTP,
* SECURITY_FTPS and SECURITY_FTPES constants. Default value is
* SECURITY_FTP.
*/
private int security = SECURITY_FTP;
/**
* If the client is authenticated, it reports the authentication username.
*/
private String username;
/**
* If the client is authenticated, it reports the authentication password.
*/
private String password;
/**
* The flag reporting the connection status.
*/
private boolean connected = false;
/**
* The flag reporting the authentication status.
*/
private boolean authenticated = false;
/**
* The flag for the passive FTP data transfer mode. Default value is true,
* cause it's usually the preferred FTP operating mode.
*/
private boolean passive = true;
/**
* The type of the data transfer contents (auto, textual, binary). The value
* should be one of {@link FTPClient#TYPE_AUTO},
* {@link FTPClient#TYPE_TEXTUAL} and {@link FTPClient#TYPE_BINARY}
* constants. Default value is TYPE_AUTO.
*/
private int type = TYPE_AUTO;
/**
* The MLSD command policy.
- 1
- 2
- 3
- 4
前往页