📄 ftpcontrolsocket.java
字号:
/**
*
* edtFTPj
*
* Copyright (C) 2000-2003 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 sent to bruce@enterprisedt.com
*
* Change Log:
*
* $Log: FTPControlSocket.java,v $
* Revision 1.17 2004/10/18 15:56:46 bruceb
* set encoding for sock, remove sendCommandOld etc
*
* Revision 1.16 2004/09/18 09:33:47 bruceb
* 1.1.8 tweaks
*
* Revision 1.15 2004/08/31 10:46:59 bruceb
* restructured reply code
*
* Revision 1.14 2004/07/23 23:29:57 bruceb
* sendcommand public again
*
* Revision 1.13 2004/07/23 08:30:40 bruceb
* restructured re non-strict replies
*
* Revision 1.12 2004/05/22 16:52:57 bruceb
* message listener
*
* Revision 1.11 2004/05/01 17:05:15 bruceb
* Logger stuff added
*
* Revision 1.10 2004/03/23 20:25:47 bruceb
* added US-ASCII to control stream constructor
*
* Revision 1.9 2003/11/15 11:23:55 bruceb
* changes required for ssl subclasses
*
* Revision 1.6 2003/05/31 14:53:44 bruceb
* 1.2.2 changes
*
* Revision 1.5 2003/01/29 22:46:08 bruceb
* minor changes
*
* Revision 1.4 2002/11/19 22:01:25 bruceb
* changes for 1.2
*
* Revision 1.3 2001/10/09 20:53:46 bruceb
* Active mode changes
*
* Revision 1.1 2001/10/05 14:42:04 bruceb
* moved from old project
*
*
*/
package com.enterprisedt.net.ftp;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.util.Vector;
import com.enterprisedt.util.debug.Logger;
/**
* Supports client-side FTP operations
*
* @author Bruce Blackshaw
* @version $Revision: 1.17 $
*
*/
public class FTPControlSocket {
/**
* Revision control id
*/
public static String cvsId = "@(#)$Id: FTPControlSocket.java,v 1.17 2004/10/18 15:56:46 bruceb Exp $";
/**
* Standard FTP end of line sequence
*/
static final String EOL = "\r\n";
/**
* The default and standard control port number for FTP
*/
public static final int CONTROL_PORT = 21;
/**
* Used to flag messages
*/
private static final String DEBUG_ARROW = "---> ";
/**
* Start of password message
*/
private static final String PASSWORD_MESSAGE = DEBUG_ARROW + "PASS";
/**
* Logging object
*/
private Logger log = Logger.getLogger(FTPControlSocket.class);
/**
* Use strict return codes if true
*/
private boolean strictReturnCodes = true;
/**
* The underlying socket.
*/
protected Socket controlSock = null;
/**
* The write that writes to the control socket
*/
protected Writer writer = null;
/**
* The reader that reads control data from the
* control socket
*/
protected BufferedReader reader = null;
/**
* Message listener
*/
private FTPMessageListener messageListener = null;
/**
* Constructor. Performs TCP connection and
* sets up reader/writer. Allows different control
* port to be used
*
* @param remoteAddr Remote inet address
* @param controlPort port for control stream
* @param millis the length of the timeout, in milliseconds
* @param encoding character encoding used for data
* @param messageListener listens for messages
*/
FTPControlSocket(InetAddress remoteAddr, int controlPort, int timeout,
String encoding, FTPMessageListener messageListener)
throws IOException, FTPException {
this(new Socket(remoteAddr, controlPort), timeout, encoding, messageListener);
}
/**
* Constructs a new <code>FTPControlSocket</code> using the given
* <code>Socket</code> object.
*
* @param controlSock Socket to be used.
* @param timeout Timeout to be used.
* @param encoding character encoding used for data
* @param messageListener listens for messages
*
* @throws IOException Thrown if no connection response could be read from the server.
* @throws FTPException Thrown if the incorrect connection response was sent by the server.
*/
protected FTPControlSocket(Socket controlSock, int timeout,
String encoding, FTPMessageListener messageListener)
throws IOException, FTPException {
this.controlSock = controlSock;
this.messageListener = messageListener;
setTimeout(timeout);
initStreams(encoding);
validateConnection();
}
/**
* Checks that the standard 220 reply is returned
* following the initiated connection
*/
private void validateConnection()
throws IOException, FTPException {
FTPReply reply = readReply();
validateReply(reply, "220");
}
/**
* Obtain the reader/writer streams for this
* connection
*
* @param encoding character encoding used for data
*/
private void initStreams(String encoding)
throws IOException {
// input stream
InputStream is = controlSock.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, encoding));
// output stream
OutputStream os = controlSock.getOutputStream();
writer = new OutputStreamWriter(os, encoding);
}
/**
* Get the name of the remote host
*
* @return remote host name
*/
String getRemoteHostName() {
InetAddress addr = controlSock.getInetAddress();
return addr.getHostName();
}
/**
* Set strict checking of FTP return codes. If strict
* checking is on (the default) code must exactly match the expected
* code. If strict checking is off, only the first digit must match.
*
* @param strict true for strict checking, false for loose checking
*/
void setStrictReturnCodes(boolean strict) {
this.strictReturnCodes = strict;
}
/**
* Set the TCP timeout on the underlying control socket.
*
* If a timeout is set, then any operation which
* takes longer than the timeout value will be
* killed with a java.io.InterruptedException.
*
* @param millis The length of the timeout, in milliseconds
*/
void setTimeout(int millis)
throws IOException {
if (controlSock == null)
throw new IllegalStateException(
"Failed to set timeout - no control socket");
controlSock.setSoTimeout(millis);
}
/**
* Set a listener that handles all FTP messages
*
* @param listener message listener
*/
void setMessageListener(FTPMessageListener listener) {
this.messageListener = listener;
}
/**
* Quit this FTP session and clean up.
*/
public void logout()
throws IOException {
IOException ex = null;
try {
writer.close();
}
catch (IOException e) {
ex = e;
}
try {
reader.close();
}
catch (IOException e) {
ex = e;
}
try {
controlSock.close();
}
catch (IOException e) {
ex = e;
}
if (ex != null)
throw ex;
}
/**
* Request a data socket be created on the
* server, connect to it and return our
* connected socket.
*
* @param active if true, create in active mode, else
* in passive mode
* @return connected data socket
*/
FTPDataSocket createDataSocket(FTPConnectMode connectMode)
throws IOException, FTPException {
if (connectMode == FTPConnectMode.ACTIVE) {
return createDataSocketActive();
}
else { // PASV
return createDataSocketPASV();
}
}
/**
* Request a data socket be created on the Client
* client on any free port, do not connect it to yet.
*
* @return not connected data socket
*/
FTPDataSocket createDataSocketActive()
throws IOException, FTPException {
// use any available port
FTPDataSocket socket = newActiveDataSocket(0);
// get the local address to which the control socket is bound.
InetAddress localhost = controlSock.getLocalAddress();
// send the PORT command to the server
setDataPort(localhost, (short)socket.getLocalPort());
return socket;
}
/**
* Helper method to convert a byte into an unsigned short value
*
* @param value value to convert
* @return the byte value as an unsigned short
*/
private short toUnsignedShort(byte value) {
return ( value < 0 )
? (short) (value + 256)
: (short) value;
}
/**
* Convert a short into a byte array
*
* @param value value to convert
* @return a byte array
*/
protected byte[] toByteArray (short value) {
byte[] bytes = new byte[2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -