Supports client-side FTP. Most common
FTP operations are present in this class.
| Method from com.enterprisedt.net.ftp.FTPClient Detail: |
public void chdir(String dir) throws IOException, FTPException {
String reply = control.sendCommand("CWD " + dir);
control.validateReply(reply, "250");
}
Change the remote working directory to
that supplied |
public void debugResponses(boolean on) {
control.debugResponses(on);
}
Switch debug of responses on or off |
public void delete(String remoteFile) throws IOException, FTPException {
String reply = control.sendCommand("DELE " + remoteFile);
control.validateReply(reply, "250");
}
Delete the specified remote file |
public String[] dir(String mask) throws IOException, FTPException {
return dir(mask, false);
}
List a directory's contents as an array of strings of filenames. |
public String[] dir(String mask,
boolean full) throws IOException, FTPException {
// set up data channel
data = control.createDataSocket(connectMode);
data.setTimeout(timeout);
// send the retrieve command
String command = full ? "LIST ":"NLST ";
command += mask;
// some FTP servers bomb out if NLST has whitespace appended
command = command.trim();
String reply = control.sendCommand(command);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
// get an character input stream to read data from ... AFTER we
// have the ok to go ahead
LineNumberReader in =
new LineNumberReader(
new InputStreamReader(data.getInputStream()));
// read a line at a time
Vector lines = new Vector();
String line = null;
while ((line = in.readLine()) != null) {
lines.add(line);
}
try {
in.close();
data.close();
}
catch (IOException ignore) {}
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return (String[])lines.toArray(new String[0]);
}
List a directory's contents as an array of strings. A detailed
listing is available, otherwise just filenames are provided.
The detailed listing varies in details depending on OS and
FTP server. |
public byte[] get(String remoteFile) throws IOException, FTPException {
// set up data channel
data = control.createDataSocket(connectMode);
data.setTimeout(timeout);
// send the retrieve command
String reply = control.sendCommand("RETR " + remoteFile);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
// get an input stream to read data from
BufferedInputStream in =
new BufferedInputStream(
new DataInputStream(data.getInputStream()));
// do the retrieving
int chunksize = 4096;
byte [] chunk = new byte[chunksize]; // read chunks into
byte [] resultBuf = new byte[chunksize]; // where we place chunks
byte [] temp = null; // temp swap buffer
int count; // size of chunk read
int bufsize = 0; // size of resultBuf
// read from socket & write to file
while ((count = in.read(chunk, 0, chunksize)) >= 0) {
// new buffer to hold current buf + new chunk
temp = new byte[bufsize+count];
// copy current buf to temp
System.arraycopy(resultBuf, 0, temp, 0, bufsize);
// copy new chunk onto end of temp
System.arraycopy(chunk, 0, temp, bufsize, count);
// re-assign temp buffer to buf
resultBuf = temp;
// update size of buffer
bufsize += count;
}
// close streams
try {
in.close();
data.close();
}
catch (IOException ignore) {}
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return resultBuf;
}
Get data from the FTP server. Transfers in
whatever mode we are in. Retrieve as a byte array. Note
that we may experience memory limitations as the
entire file must be held in memory at one time. |
public void get(String localPath,
String remoteFile) throws IOException, FTPException {
// get according to set type
if (getType() == FTPTransferType.ASCII) {
getASCII(localPath, remoteFile);
}
else {
getBinary(localPath, remoteFile);
}
// check the control response
String[] validCodes2 = {"226", "250"};
String reply = control.readReply();
control.validateReply(reply, validCodes2);
}
Get data from the FTP server. Uses the currently
set transfer mode. |
public InputStream get(String remoteFile,
long byteSkip) throws IOException, FTPException {
String reply = "";
// set up data channel
data = control.createDataSocket(connectMode);
data.setTimeout(timeout);
if(byteSkip > 0) {
// send the rest command
reply = control.sendCommand("REST " + byteSkip);
// Can get a 350
String validCodes1 = "350";
control.validateReply(reply, validCodes1);
}
// send the retrieve command
reply = control.sendCommand("RETR " + remoteFile);
// Can get a 125 or a 150
String[] validCodes2 = {"125", "150"};
control.validateReply(reply, validCodes2);
return data.getInputStream();
}
Get data from the FTP server. Transfers in
whatever mode we are in. Returns the
InputStream for the FTP connection. |
public int getFileSize(String remoteFile) throws IOException, FTPException {
String reply = "";
reply = control.sendCommand("SIZE " + remoteFile);
// Can get a 213
String validCodes = "213";
control.validateReply(reply, validCodes);
return Integer.parseInt(reply.substring(4));
}
|
String getRemoteHostName() {
return control.getRemoteHostName();
}
Get the name of the remote host |
public FTPTransferType getType() {
return transferType;
}
Get the current transfer type |
public void initSOCKS(String port,
String host) {
Properties props = System.getProperties();
props.put("socksProxyPort", port);
props.put("socksProxyHost", host);
System.setProperties(props);
}
Set up SOCKS v4 proxy settings. This can be used if there
is a SOCKS proxy server in place that must be connected thru. |
public String list(String mask) throws IOException, FTPException {
return list(mask, false);
} Deprecated! As - of FTP 1.1, replaced by #dir(String)
List a directory's contents |
public String list(String mask,
boolean full) throws IOException, FTPException {
String[] list = dir(mask, full);
StringBuffer result = new StringBuffer();
String sep = System.getProperty("line.separator");
// loop thru results and make into one string
for (int i = 0; i < list.length; i++) {
result.append(list[i]);
result.append(sep);
}
return result.toString();
} Deprecated! As - of FTP 1.1, replaced by #dir(String,boolean)
List a directory's contents as one string. A detailed
listing is available, otherwise just filenames are provided.
The detailed listing varies in details depending on OS and
FTP server. |
public void login(String user,
String password) throws IOException, FTPException {
String response = control.sendCommand("USER " + user);
control.validateReply(response, "331");
response = control.sendCommand("PASS " + password);
control.validateReply(response, "230");
}
Login into an account on the FTP server. This
call completes the entire login process |
public void mkdir(String dir) throws IOException, FTPException {
String reply = control.sendCommand("MKD " + dir);
control.validateReply(reply, "257");
}
Create the specified remote working directory |
public void password(String password) throws IOException, FTPException {
String reply = control.sendCommand("PASS " + password);
// we allow for a site with no passwords (202)
String[] validCodes = {"230", "202"};
control.validateReply(reply, validCodes);
}
Supplies the password for a previously supplied
username to log into the FTP server. Must be
preceeded by the user() method |
public void put(String localPath,
String remoteFile) throws IOException, FTPException {
put(localPath, remoteFile, false);
}
Put a local file onto the FTP server. It
is placed in the current directory. |
public void put(byte[] bytes,
String remoteFile) throws IOException, FTPException {
put(bytes, remoteFile, false);
}
Put data onto the FTP server. It
is placed in the current directory. |
public void put(String localPath,
String remoteFile,
boolean append) throws IOException, FTPException {
// get according to set type
if (getType() == FTPTransferType.ASCII) {
putASCII(localPath, remoteFile, append);
}
else {
putBinary(localPath, remoteFile, append);
}
// check the control response
String[] validCodes2 = {"226", "250"};
String reply = control.readReply();
control.validateReply(reply, validCodes2);
}
Put a local file onto the FTP server. It
is placed in the current directory. Allows appending
if current file exists |
public void put(byte[] bytes,
String remoteFile,
boolean append) throws IOException, FTPException {
initPut(remoteFile, append);
// get an output stream
BufferedOutputStream out =
new BufferedOutputStream(
new DataOutputStream(data.getOutputStream()));
// write array
out.write(bytes, 0, bytes.length);
// flush and clean up
out.flush();
out.close();
// and close the data socket
try {
data.close();
}
catch (IOException ignore) {}
// check the control response
String[] validCodes2 = {"226", "250"};
String reply = control.readReply();
control.validateReply(reply, validCodes2);
}
Put data onto the FTP server. It
is placed in the current directory. Allows
appending if current file exists |
public String pwd() throws IOException, FTPException {
String reply = control.sendCommand("PWD");
control.validateReply(reply, "257");
return reply.substring(4);
}
Get the current remote working directory |
public void quit() throws IOException, FTPException {
String reply = control.sendCommand("QUIT" + FTPControlSocket.EOL);
control.validateReply(reply, "221");
control.logout();
control = null;
}
|
public void quote(String command,
String[] validCodes) throws IOException, FTPException {
String reply = control.sendCommand(command);
// allow for no validation to be supplied
if (validCodes != null && validCodes.length > 0)
control.validateReply(reply, validCodes);
}
Issue arbitrary ftp commands to the FTP server. |
public void rename(String from,
String to) throws IOException, FTPException {
String reply = control.sendCommand("RNFR " + from);
control.validateReply(reply, "350");
reply = control.sendCommand("RNTO " + to);
control.validateReply(reply, "250");
}
Rename a file or directory |
public void rmdir(String dir) throws IOException, FTPException {
String reply = control.sendCommand("RMD " + dir);
control.validateReply(reply, "250");
}
Delete the specified remote working directory |
public void setConnectMode(FTPConnectMode mode) {
connectMode = mode;
}
|
public void setTimeout(int millis) throws IOException {
this.timeout = millis;
control.setTimeout(millis);
}
Set the TCP timeout on the underlying socket.
If a timeout is set, then any operation which
takes longer than the timeout value will be
killed with a java.io.InterruptedException. We
set both the control and data connections |
public void setType(FTPTransferType type) throws IOException, FTPException {
// determine the character to send
String typeStr = FTPTransferType.ASCII_CHAR;
if (type.equals(FTPTransferType.BINARY))
typeStr = FTPTransferType.BINARY_CHAR;
// send the command
String reply = control.sendCommand("TYPE " + typeStr);
control.validateReply(reply, "200");
// record the type
transferType = type;
}
|
public boolean site(String command) throws IOException, FTPException {
// send the retrieve command
String reply = control.sendCommand("SITE " + command);
// Can get a 200 (ok) or 202 (not impl). Some
// FTP servers return 502 (not impl)
String[] validCodes = {"200", "202", "502"};
control.validateReply(reply, validCodes);
// return true or false? 200 is ok, 202/502 not
// implemented
if (reply.substring(0, 3).equals("200"))
return true;
else
return false;
}
Run a site-specific command on the
server. Support for commands is dependent
on the server |
public String system() throws IOException, FTPException {
String reply = control.sendCommand("SYST");
control.validateReply(reply, "215");
return reply.substring(4);
}
Get the type of the OS at the server |
public void user(String user) throws IOException, FTPException {
String reply = control.sendCommand("USER " + user);
// we allow for a site with no password - 230 response
String[] validCodes = {"230", "331"};
control.validateReply(reply, validCodes);
}
Supply the user name to log into an account
on the FTP server. Must be followed by the
password() method - but we allow for |