import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
public class SimpleFtpClient
{
private FTPClient ftpClient;
public void connectServer(String server, int port, String user,
String password) throws SocketException, IOException
{
this.connectServer(server, port, user, password, null);
}
public void connectServer(String server, int port, String user,
String password, String path) throws SocketException, IOException
{
ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
ftpClient.connect(server, port);
// System.out.println("Connected to " + server + ":" + port);
// System.out.println(ftpClient.getReplyCode());
ftpClient.login(user, password);
if (null != path && 0 != path.length())
{
ftpClient.changeWorkingDirectory(path);
// System.out.println("wordDir:" + path);
}
// default bin
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
public void closeConnect() throws IOException
{
ftpClient.logout();
if (ftpClient.isConnected())
{
ftpClient.disconnect();
}
}
public boolean changeDirectory(String path) throws IOException
{
return ftpClient.changeWorkingDirectory(path);
}
public boolean deleteFile(String pathName) throws IOException
{
return ftpClient.deleteFile(pathName);
}
public boolean uploadFile(String filePathName, String newName)
throws IOException
{
boolean flag = false;
InputStream iStream = null;
try
{
iStream = new FileInputStream(filePathName);
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e)
{
flag = false;
return flag;
} finally
{
if (iStream != null)
{
iStream.close();
}
}
return flag;
}
public boolean uploadFile(InputStream iStream, String newName)
throws IOException
{
boolean flag = false;
try
{
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e)
{
flag = false;
return flag;
} finally
{
if (iStream != null)
{
iStream.close();
}
}
return flag;
}
public boolean download(String remoteFileName, String localFileName)
throws IOException
{
boolean flag = false;
File outfile = new File(localFileName);
OutputStream oStream = null;
try
{
oStream = new FileOutputStream(outfile);
flag = ftpClient.retrieveFile(remoteFileName, oStream);
} catch (IOException e)
{
flag = false;
return flag;
} finally
{
oStream.close();
}
return flag;
}
public InputStream downFile(String sourceFileName) throws IOException
{
return ftpClient.retrieveFileStream(sourceFileName);
}
public FTPClient getFtpClient()
{
return ftpClient;
}
/**
* 是否不存在
*/
public boolean isNotExist(String fileName) throws IOException
{
String[] listNames = ftpClient.listNames();
for (int i = 0; i < listNames.length; i++)
{
if (listNames.equals(fileName))
{
return false;
}
}
return true;
}
/**
* 获得相类似的文件名
*/
public String getSimlarFileName(String fileName) throws IOException
{
String[] listNames = ftpClient.listNames();
for (int i = 0; i < listNames.length; i++)
{
if (listNames.indexOf(fileName) >= 0)
{
return listNames;
}
}
return null;
}
/**
* 打印当前工作目录
*/
public String printWorkingDirectory() throws IOException
{
return ftpClient.printWorkingDirectory();
}
/**
* 两个ftp之间传输
*/
public static boolean transFromftpToftp(SimpleFtpClient srcSimpleFtpClient,
SimpleFtpClient dstSimpleFtpClient, String srcFileSimpleName,
String dstFileSimpleName)
{
if (null == srcSimpleFtpClient || null == dstSimpleFtpClient)
{
return false;
}
FTPClient srcFtpClient = srcSimpleFtpClient.getFtpClient();
FTPClient dstFtpClient = dstSimpleFtpClient.getFtpClient();
if (null == srcFtpClient || null == dstFtpClient)
{
return false;
}
boolean flag = false;
InputStream retrieveFileStream = null;
try
{
retrieveFileStream = srcFtpClient
.retrieveFileStream(srcFileSimpleName);
dstFtpClient.storeFile(dstFileSimpleName, retrieveFileStream);
flag = true;
} catch (IOException e)
{
flag = false;
return flag;
} finally
{
try
{
retrieveFileStream.close();
} catch (IOException ignore)
{
// NO OP
}
}
return flag;
}
/**
* 重命名
*/
public boolean reName( String oldName, String newName) throws IOException
{
boolean flag = false;
try
{
flag = ftpClient.rename(oldName, newName);
}
catch (IOException e)
{
flag = false;
return flag;
}
finally
{
}
return flag;
}
// public static void main(String[] args)
// throws SocketException, IOException
// {
// SimpleFtpClient client = new SimpleFtpClient();
//
// String server = "10.168.69.224";
//
// client.connectServer(server, 21, "hmsftp", "hmsftp", "test/sub");
//
// String localFileName = "localKeep.txt";
// // client.download("wenzhou.mpg", "D://ftp//wenzhou.mpg");
// // client.uploadFile("D://ftp//lin.ts","linxxx.ts");
// client.deleteFile("toobig1.ts");
//
// client.closeConnect();
// }
}