📄 jmyftpclient.java
字号:
/*
* Created on 2005-6-28
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author CN2WW0D0
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class JMyFtpClient
{
/**
*
*/
public JMyFtpClient()
{
super();
// TODO Auto-generated constructor stub
m_FtpClient = null;
}
public static void main(String[] args) throws ClassNotFoundException, IOException
{
System.out.println( "JMyFtpClient Started" );
JMyFtpClient ftp = new JMyFtpClient();
if ( false == ftp.Open( "172.16.64.21" , 21 , "application" , "application" ) )
{
System.out.println( "OpenFail" );
return;
}
if ( false == ftp.DownloadDirectory( "/appl/Tmpww" , "C:\\TmpWw\\Down" ) )
trace( "download Fail" );
/* if ( false == ftp.UploadDirectory( "C:\\TmpWw" , "/appl/Tmpww") )
trace( "Uplaod Directpry Fail" );
else
trace( "Upload Directory Success" );
*/
/* for ( int i=0; i < 0; i++ )
{
int nByteDown = ftp.DownloadFile( "appl/3.txt" , "c:\\TmpWw\\Temp\\12" + i + ".txt" );
if ( nByteDown <= 0 )
System.out.println( "Download File Fail" );
else
trace( "Download File Success" );
int nByteUp = ftp.UploadFile( "C:\\TmpWw\\1.txt" , "appl/Tmp/Tmp2/1" + i + ".txt" );
if ( nByteUp <= 0 )
System.out.println( "Upload File Fail" );
else
trace( "Upload File Success" );
}
*/
ftp.Close();
System.out.println( "JMyFtpClient Stopped" );
}
/*Open Connection*/
public boolean Open( String strServIPAddr , int nServPort ,
String strUserName , String strPassword )
{
try
{
m_FtpClient = new FTPClientDrv( strServIPAddr , nServPort ); //Connect to Server);
m_FtpClient.login( strUserName , strPassword );
m_FtpClient.setType(new FTPTransferType());
}
catch (IOException e)
{
trace( "Open Ftp Server Fail, IOException" );
e.printStackTrace();
return false;
} catch (FTPException e)
{
trace( "Open Ftp server Fail, Ftp Exception" );
e.printStackTrace();
return false;
}
trace( "Login FTP Server Ok" );
return true;
}
/**
* List current wroking directory's content
* @param strDir Root directory
*/
public String ListFile( String strDir )
{
String strList = strDir;
try
{
strList = m_FtpClient.list( strList , true );
}
catch ( IOException e )
{
e.printStackTrace();
return "";
}
catch ( FTPException e)
{
e.printStackTrace();
return "";
}
return strList;
}
/*
* Download File
*
* */
public int DownloadFile( String strSrcFile , String strDstFile )
{
trace( "Begin Downloading File " + strSrcFile + " to " + strDstFile );
int nRead = 0;
int nTmpRead = 0;
File f = new File( StringTrimHelper.GetPathName(strDstFile) );
trace( "File Root Path = " + f.getPath() );
f.mkdirs();
try
{
nRead = m_FtpClient.get( strSrcFile , strDstFile );
trace( "Download File Length in Bytes = " + nRead );
}
catch (IOException e)
{
e.printStackTrace();
return -1;
}
catch (FTPException e)
{
e.printStackTrace();
return -1;
}
return nRead;
}
/*
* UploadFile
* */
public int UploadFile( String strSrcFile , String strDstFile )
{
int nWrite = 0;
try
{
File f = new File( strSrcFile );
if ( false == f.exists() )
{
trace( "SourceFile NOT exist! : " + strSrcFile );
return -1;
}
trace( "Current Working Directory =" + m_FtpClient.pwd() );
String strPathName = StringTrimHelper.GetUrlPathName( strDstFile );
trace( strPathName );
Mkdirs( strPathName );
nWrite = m_FtpClient.put( strSrcFile , strDstFile );
trace( "Upload File Length in Bytes = " + nWrite );
}
catch( IOException e)
{
e.printStackTrace();
return -1;
}
catch (FTPException e)
{
e.printStackTrace();
return -1;
}
return nWrite;
}
/**
*
* @param strSrcRootPath source path -local
* @param strDstRootPath dest path -remote
* @return true for success, false for fail
*/
public boolean UploadDirectory( String strSrcRootPath , String strDstRootPath )
{
File fSrc = new File( strSrcRootPath );
if ( false == fSrc.exists() || false == fSrc.isDirectory() )
return false;
// Remove the remote Dst directory first.
RemoveDirectory( strDstRootPath , true );
// Upload file One by One
String [] astrSrcFile = fSrc.list();
for ( int i=0; i<astrSrcFile.length; i++ )
{
String strSrcFileTmp = strSrcRootPath + "\\" + astrSrcFile[i];
String strDstFileTmp = strDstRootPath + "/" + astrSrcFile[i];
File fSrcTmp = new File( strSrcFileTmp );
if ( fSrcTmp.isDirectory() )
{
Mkdirs( strDstFileTmp );
if ( false == UploadDirectory( strSrcFileTmp , strDstFileTmp ) )
return false;
continue;
}
if ( fSrcTmp.isFile() )
{
if ( UploadFile( strSrcFileTmp , strDstFileTmp ) < 0 )
return false;
continue;
}
trace( "Error! Unknown File Type : " + strSrcFileTmp + " ; Upload Directory Fail" );
return false;
}
return true;
}
/**
*
* @param strSrcRootPath the remote ftp path
* @param strDstRootPath the local unc path
* @return true for success, false for fail
*/
public boolean DownloadDirectory( String strSrcRootPath , String strDstRootPath )
{
String [] astrFilePath = null;
try {
astrFilePath = m_FtpClient.list( strSrcRootPath );
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FTPException e) {
e.printStackTrace();
return false;
}
//Remove local directory
if ( strDstRootPath.length() > 3 ) // for case "C:\\" ,never remove system root
DirectoryHelper.zeroDirectory( strDstRootPath );
for ( int i=1; i<astrFilePath.length; i++ ) //astrFilePath[0] is : "total X"
{
String strRawFileInfo = astrFilePath[i].trim(); //trim white space
String strFilePathTmp = strRawFileInfo.substring(
strRawFileInfo.lastIndexOf(" ")+1 ,
strRawFileInfo.length() );
if ( 0 == strFilePathTmp.compareToIgnoreCase(".")
|| 0 == strFilePathTmp.compareToIgnoreCase("..") )
continue;
char chFlag = astrFilePath[i].charAt(0);
if ( 'd' == chFlag )
{ //It is directory
if ( false == DownloadDirectory( strSrcRootPath + "/" + strFilePathTmp ,
strDstRootPath + "\\" + strFilePathTmp ) )
return false;
continue;
}
//Now is file
if ( 0 > DownloadFile( strSrcRootPath + "/" + strFilePathTmp, strDstRootPath + "\\" + strFilePathTmp ) )
{
trace( "Download File Fail: " + strSrcRootPath + "/" + strFilePathTmp);
return false;
}
continue;
}
return true;
}
/**
*
* @param strFilePath the remote file to be del
* @return true for success, false for fail
*/
public boolean DeleteFile( String strFilePath )
{
try {
m_FtpClient.delete( strFilePath );
return true;
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
return false;
}
public boolean RemoveDirectory( String strRootPath , boolean bRmRoot )
{
boolean bVal = DeleteDirectory( strRootPath );
if ( bRmRoot )
{
try {
m_FtpClient.rmdir( strRootPath );
} catch (IOException e) {
bVal = false;
} catch (FTPException e) {
bVal = false;
}
}
return bVal;
}
/**
*
* @param strRootPath the dir to me del
* @return true for success, false for fail
*/
private boolean DeleteDirectory( String strRootPath )
{
trace( "Begin Del Directory : " + strRootPath );
String [] astrFilePath = null;
try {
astrFilePath = m_FtpClient.list( strRootPath );
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (FTPException e) {
e.printStackTrace();
return false;
}
for ( int i=1; i<astrFilePath.length; i++ ) //astrFilePath[0] is : "total X"
{
String strRawFileInfo = astrFilePath[i].trim(); //trim white space
String strFilePathTmp = strRawFileInfo.substring(
strRawFileInfo.lastIndexOf(" ")+1 ,
strRawFileInfo.length() );
if ( 0 == strFilePathTmp.compareToIgnoreCase(".")
|| 0 == strFilePathTmp.compareToIgnoreCase("..") )
continue;
char chFlag = astrFilePath[i].charAt(0);
if ( 'd' == chFlag )
{ //It is directory
DeleteDirectory( strRootPath + "/" + strFilePathTmp );
try {
m_FtpClient.rmdir( strRootPath + "/" + strFilePathTmp );
} catch (IOException e1) {
e1.printStackTrace();
return false;
} catch (FTPException e1) {
e1.printStackTrace();
return false;
}
continue;
}
//Now is file
if ( false == DeleteFile( strRootPath + "/" + strFilePathTmp ) )
{
trace( "Delete File Fail: " + strRootPath + "/" + strFilePathTmp);
return false;
}
continue;
}
return true;
}
/**
Just Mk root dir
*/
public void Mkdirs( String strRootPath )
{
if ( strRootPath.length() < 1 )
return;
int nIndex = strRootPath.indexOf( '/' , 1 );
if ( -1 == nIndex )
{
MkdirOneLayer( strRootPath );
return;
}
String strTmp ;
while ( -1 != nIndex )
{
strTmp = strRootPath.substring( 0 , nIndex );
MkdirOneLayer( strTmp );
nIndex = strRootPath.indexOf( '/' , nIndex+1 );
}
MkdirOneLayer( strRootPath ); //The last layer!
}
private void MkdirOneLayer( String strOneLayer )
{
try {
m_FtpClient.mkdir( strOneLayer );
} catch (IOException e) {
e.printStackTrace();
} catch (FTPException e) {
trace( "Directory Maybe already created : " + strOneLayer );
}
}
/*Close Connection*/
public void Close()
{
if ( null != m_FtpClient )
{
try
{
m_FtpClient.quit();
m_FtpClient = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -