📄 filetransferclient.java
字号:
/**
*
* Copyright (C) 2007 Enterprise Distributed Technologies Ltd
*
* www.enterprisedt.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Bug fixes, suggestions and comments should be should posted on
* http://www.enterprisedt.com/forums/index.php
*
* Change Log:
*
* $Log: FileTransferClient.java,v $
* Revision 1.13 2008-06-03 03:20:15 bruceb
* cleanup
*
* Revision 1.12 2008-06-03 03:17:59 bruceb
* clear stats on connect
*
* Revision 1.11 2008-06-02 06:50:17 bruceb
* fix bug re messages not being set
*
* Revision 1.10 2008-05-23 02:45:41 bruceb
* moved context
*
* Revision 1.9 2008-05-22 04:20:55 bruceb
* moved stuff to internal etc
*
* Revision 1.8 2008-05-02 07:41:30 bruceb
* setModTime added
*
* Revision 1.7 2008-03-31 02:16:04 bruceb
* fix event listener assignment
*
* Revision 1.6 2008-03-31 01:18:02 bruceb
* fix isconnected bug
*
* Revision 1.5 2008-03-31 00:16:23 bruceb
* advanced settings rejig
*
* Revision 1.4 2008-03-13 00:22:53 bruceb
* various tweaks for j/pro
*
* Revision 1.3 2008-01-09 03:54:21 bruceb
* executeCommand() now returns reply code
*
* Revision 1.2 2007-12-20 00:40:16 bruceb
* autologin
*
* Revision 1.1 2007-12-18 07:52:06 bruceb
* 2.0 changes
*
*
*/
package com.enterprisedt.net.ftp;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import com.enterprisedt.net.ftp.internal.ConnectionContext;
import com.enterprisedt.net.ftp.internal.EventAggregator;
import com.enterprisedt.util.debug.Logger;
/**
* Easy to use FTP client that is thread safe and provides true FTP streams.
* This class is intended to replace FTPClient, which will eventually be
* deprecated.
*
* @author Bruce Blackshaw
* @version $Revision: 1.13 $
*/
public class FileTransferClient implements FileTransferClientInterface {
private Logger log = Logger.getLogger("FileTransferClient");
/**
* Context for the client that is the starting point for all
* new tasks. If we have a change directory task, when it is completed
* it will update this master context and the updated context will be used
* for all future tasks
*/
protected ConnectionContext masterContext = new ConnectionContext();
protected EventAggregator eventAggregator = null;
/**
* Event listeners
*/
protected EventListener listener;
/**
* Instance of FTPClient
*/
private FTPClient ftpClient;
/**
* Advanced configuration parameters that often aren't used
*/
private AdvancedFTPSettings advancedFTPSettings;
private AdvancedGeneralSettings advancedSettings;
private FileStatistics statistics;
/**
* Default constructor
*/
public FileTransferClient() {
ftpClient = new FTPClient();
advancedFTPSettings = new AdvancedFTPSettings(masterContext);
advancedSettings = new AdvancedGeneralSettings(masterContext);
statistics = new FileStatistics();
statistics.addClient(ftpClient);
}
/**
* Checks if the client has connected to the server and throws an exception if it hasn't.
* This is only intended to be used by subclasses
*
* @throws FTPException Thrown if the client has not connected to the server.
*/
protected void checkConnection(boolean shouldBeConnected) throws FTPException {
if (shouldBeConnected && !isConnected())
throw new FTPException("The file transfer client has not yet connected to the server. "
+ "The requested action cannot be performed until after a connection has been established.");
else if (!shouldBeConnected && isConnected())
throw new FTPException("The file transfer client has already been connected to the server. "
+"The requested action must be performed before a connection is established.");
}
/**
* Is this client currently connected to the server?
*
* @return true if connected, false otherwise
*/
public synchronized boolean isConnected() {
return ftpClient.connected();
}
/**
* Returns the IP address or name of the remote host.
*
* @return Returns the remote host.
*/
public synchronized String getRemoteHost() {
return masterContext.getRemoteHost();
}
/**
* Set the IP address or name of the remote host
*
* This may only be done if the client is not already connected to the server.
*
* @param remoteHost The IP address or name of the remote host
* @throws FTPException Thrown if the client is already connected to the server.
*/
public synchronized void setRemoteHost(String remoteHost) throws FTPException {
checkConnection(false);
masterContext.setRemoteHost(remoteHost);
}
/**
* Returns the timeout for socket connections.
*
* @return Returns the connection timeout in milliseconds
*/
public synchronized int getTimeout() {
return masterContext.getTimeout();
}
/**
* Set the timeout for socket connections. Can only do this if
* not already connected.
*
* @param timeout the timeout to use in milliseconds
* @throws FTPException Thrown if the client is already connected to the server.
*/
public synchronized void setTimeout(int timeout) throws FTPException {
checkConnection(false);
masterContext.setTimeout(timeout);
}
/**
* Returns the port being connected to on the remote server.
*
* @return Returns the port being connected to on the remote server.
*/
public synchronized int getRemotePort() {
return masterContext.getRemotePort();
}
/**
* Set the port to connect to on the remote server. Can only do this if
* not already connected.
*
* @param remotePort The port to use.
* @throws FTPException Thrown if the client is already connected to the server.
*/
public synchronized void setRemotePort(int remotePort) throws FTPException {
checkConnection(false);
masterContext.setRemotePort(remotePort);
}
/**
* Set the transfer type for all connections, either ASCII or binary. Setting
* applies to all subsequent transfers that are initiated.
*
* @param type transfer type
* @throws FTPException
* @throws IOException
* @throws FTPException
*/
public synchronized void setContentType(FTPTransferType type)
throws IOException, FTPException {
masterContext.setContentType(type);
if (ftpClient != null && ftpClient.connected())
ftpClient.setType(type);
}
/**
* Get the current content type for all connections.
*
* @return transfer type
*/
public synchronized FTPTransferType getContentType() {
return masterContext.getContentType();
}
/**
* Set auto detect of filetypes on or off. If on, the transfer mode is
* switched from ASCII to binary and vice versa depending on the extension
* of the file. After the transfer, the mode is always returned to what it
* was before the transfer was performed. The default is off.
*
* If the filetype is unknown, the transfer mode is unchanged
*
* @param detectContentType true if detecting content type, false if not
*/
public void setDetectContentType(boolean detectContentType) {
masterContext.setDetectContentType(detectContentType);
if (ftpClient != null)
ftpClient.setDetectTransferMode(detectContentType);
}
/**
* Get the detect content type flag
*
* @return true if we are detecting binary and ASCII transfers from the file type
*/
public boolean isDetectContentType() {
return masterContext.getDetectContentType();
}
/**
* Set the name of the user to log in with. Can only do this if
* not already connected.
*
* @param userName user-name to log in with.
* @throws FTPException
*/
public synchronized void setUserName(String userName) throws FTPException {
checkConnection(false);
masterContext.setUserName(userName);
}
/**
* Get the current user password.
*
* @return current user password
*/
public synchronized String getPassword() {
return masterContext.getPassword();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -