jmyftpclient.java
来自「my ftp client basic and easy」· Java 代码 · 共 1,670 行 · 第 1/4 页
JAVA
1,670 行
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( FTPException eFtp )
{
}
}
}
public static void trace( String s )
{
System.out.println( s );
}
/*Members*/
private FTPClientDrv m_FtpClient;
}
/**/
class StringTrimHelper
{
public static String TrimLastSplitter( String strSrc )
{
strSrc.trim();
if ( strSrc.lastIndexOf("\\") != strSrc.length()-1 )
return strSrc;
int nIndex = strSrc.lastIndexOf( "\\" );
return strSrc.substring(0 , nIndex );
}
public static String GetPathName( String strSrc )
{
String strTmp = TrimLastSplitter( strSrc );
return strTmp.substring( 0, strTmp.lastIndexOf("\\"));
}
public static String GetUrlPathName( String strSrc )
{
if ( strSrc.lastIndexOf("/") == strSrc.length()-1 )
strSrc = strSrc.substring( 0 , strSrc.lastIndexOf("/") );
return strSrc.substring( 0, strSrc.lastIndexOf("/") );
}
}
/**
*
* @author CN2WW0D0
*
* TODO Make directorys
*/
class DirectoryHelper
{
public static void mkdirs( String strRootPath )
{
File fTmp = new File(strRootPath);
fTmp.mkdirs();
}
public static void zeroDirectory( String strRootPath )
{
File fSrc = new File( strRootPath );
if ( false == fSrc.exists() || false == fSrc.isDirectory() )
return;
String [] astrFileList = fSrc.list();
for( int i=0; i<astrFileList.length ; i++ )
{
String strSrcFileTmp = strRootPath + "\\" + astrFileList[i]; //Get The File Abs path
String strRelaFilePath = astrFileList[i];
File fTmp = new File( strSrcFileTmp );
if ( fTmp.isFile() )
{
if ( false == fTmp.delete() )
System.out.println( "delete file fail : " + strSrcFileTmp );
continue;
}
if ( fTmp.isDirectory() )
{
zeroDirectory( strSrcFileTmp );
if ( false == fTmp.delete() )
System.out.println( "remove directory fail : " + strSrcFileTmp );
continue;
}
System.out.println( "UnknowFile : " + strSrcFileTmp );
}
}
}
/**
* Supports client-side FTP. Most common
* FTP operations are present in this class.
* Lots to do, but works ok.
*
* @author Bruce Blackshaw
* @version $Revision: 1.1.1.1 $
*
*/
class FTPClientDrv
{
public static void trace( String s )
{
System.out.println( s );
}
/**
* Revision control id
*/
private static String cvsId = "$Id: FTPClient.java,v 1.1.1.1 2001/11/06 10:36:52 idanso Exp $";
/**
* Socket responsible for controlling
* the connection
*/
private FTPControlSocket control = null;
/**
* Socket responsible for transferring
* the data
*/
private Socket data = null;
public short tmode=-1; // Data transfer mode. 0=GET 1=PUT -1=none
public long tbytes=0; // Total bytes to transfer
public long sbytes=0; // Number of bytes transferred
/**
* Record of the transfer type
*/
private FTPTransferType transferType = null;
/**
* Constructor. Creates the control
* socket
*
* @param remoteHost the remote hostname
*/
public FTPClientDrv(String remoteHost)
throws IOException, FTPException {
control = new FTPControlSocket(remoteHost);
}
/**
* Constructor. Creates the control
* socket
*
* @param remoteHost the remote hostname
* @param controlPort port for control stream
*/
public FTPClientDrv(String remoteHost, int controlPort)
throws IOException, FTPException {
control = new FTPControlSocket(remoteHost, controlPort);
}
/**
* Constructor. Creates the control
* socket
*
* @param remoteAddr the address of the
* remote host
*/
public FTPClientDrv(InetAddress remoteAddr)
throws IOException, FTPException {
control = new FTPControlSocket(remoteAddr);
}
/**
* Constructor. Creates the control
* socket. Allows setting of control port (normally
* set by default to 21).
*
* @param remoteAddr the address of the
* remote host
* @param controlPort port for control stream
*/
public FTPClientDrv(InetAddress remoteAddr, int controlPort)
throws IOException, FTPException {
control = new FTPControlSocket(remoteAddr, controlPort);
}
/**
* Login into an account on the FTP server. This
* call completes the entire login process
*
* @param user user name
* @param password user's password
*/
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");
}
/**
* Supply the user name to log into an account
* on the FTP server. Must be followed by the
* password() method - but we allow for
*
* @param user user name
* @param password user's password
*/
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);
}
/**
* Supplies the password for a previously supplied
* username to log into the FTP server. Must be
* preceeded by the user() method
*
* @param user user name
* @param password user's password
*/
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);
}
/**
* Set up SOCKS v4 proxy settings. This can be used if there
* is a SOCKS proxy server in place that must be connected thru.
*
* @param port SOCKS proxy port
* @param host SOCKS proxy hostname
*/
public void initSOCKS(String port, String host) {
Properties props = System.getProperties();
props.put("socksProxyPort", port);
props.put("socksProxyHost", host);
System.setProperties(props);
}
/**
* Get the name of the remote host
*
* @return remote host name
*/
String getRemoteHostName() {
return control.getRemoteHostName();
}
/**
* Issue arbitrary ftp commands to the FTP server.
*
* @param command ftp command to be sent to server
* @param validCodes valid return codes for this command
*/
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);
}
/*
* PutFile Directly
* */
public int put( String strSrcFile , String strDstFile ) throws IOException, FTPException
{
trace( "Entering put function......" );
// get an output stream
data = control.createDataSocket();
DataOutputStream out = new DataOutputStream(data.getOutputStream());
// send the command to store
String cmd = "STOR ";
String reply = control.sendCommand(cmd + strDstFile);
trace( "Sending to Server Cmd : " + cmd + strDstFile );
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
trace( "Validated Reply, Begin Upload File..." );
byte[] buf=new byte[1024*4];
int nRead = 0;
int nTmpRead = 0;
RandomAccessFile fSrc = new RandomAccessFile( strSrcFile , "r");
// write stream
nTmpRead = fSrc.read( buf );
while(nTmpRead > 0 )
{
out.write(buf,0,nTmpRead );
nRead += nTmpRead;
nTmpRead = fSrc.read( buf );
}
fSrc.close();
// flush and clean up
out.flush();
out.close();
// and close the data socket
try
{
data.close();
}
catch (IOException ignore)
{
return -1;
}
tmode=-1;
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return nRead;
}
/**
* Put data onto the FTP server. It
* is placed in the current directory.
*
* @param data array of bytes
* @param remoteFile name of remote file in
* current directory
*/
public void put(InputStream in, String remoteFile)
throws IOException, FTPException {
put(in, remoteFile, false);
}
/**
* Put data onto the FTP server. It
* is placed in the current directory. Allows
* appending if current file exists
*
* @param data array of bytes
* @param remoteFile name of remote file in
* current directory
* @param append true if appending, false otherwise
*/
public void put(InputStream in, String remoteFile, boolean append)
throws IOException, FTPException {
// get an output stream
data = control.createDataSocket();
DataOutputStream out = new DataOutputStream(data.getOutputStream());
// send the command to store
String cmd = append ? "APPE " : "STOR ";
String reply = control.sendCommand(cmd + remoteFile);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
byte[] buf=new byte[4096];
int len;
tmode=1;
tbytes=in.available();
sbytes=0;
// write stream
len=in.read(buf);
while(len>-1)
{
out.write(buf,0,len);
sbytes+=len;
len=in.read(buf);
}
// flush and clean up
out.flush();
out.close();
// and close the data socket
try {
data.close();
}
catch (IOException ignore) {}
tmode=-1;
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
}
/**
* GetFile
* */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?