用代码走进Ftp

因为最近做一个关于集中采集的ftp改造开发。所以研究了哈ftp的开发。

一个简单常用的连接ftp的命令:ftp 主机ip

下面贴出我自己的ftp的demo。

1、FtpUtil工具类

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;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

public class FtpUtil {
private FTPClient ftpClient;
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;

/**
* 连接FTP
* @param ftpConfig
* @throws SocketException
* @throws IOException
*/
public void connectServer(FTPConfig ftpConfig) throws SocketException,
IOException {
String server = ftpConfig.getServer();
int port = ftpConfig.getPort();
String user = ftpConfig.getUsername();
String password = ftpConfig.getPassword();
String location = ftpConfig.getLocation();
connectServer(server, port, user, password, location);
}

/**
* 连接FTP
* @param server
* @param port
* @param user
* @param password
* @param path
* @throws SocketException
* @throws IOException
*/
public void connectServer(String server, int port, String user,
String password, String path) throws SocketException, IOException {
ftpClient = new FTPClient();
ftpClient.connect(server, port);
System.out.println("Connected to " + server + ".");
System.out.println(ftpClient.getReplyCode());
ftpClient.login(user, password);
// Path is the sub-path of the FTP path
if (path.length() != 0) {
ftpClient.changeWorkingDirectory(path);
}
}
/**
* FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
* 设置传输类型
* @param fileType
* @throws IOException
*/
public void setFileType(int fileType) throws IOException {
ftpClient.setFileType(fileType);
}

public void closeServer() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}

/**
* 改变目录
* @param path
* @return
* @throws IOException
*/
public boolean changeDirectory(String path) throws IOException {
return ftpClient.changeWorkingDirectory(path);
}
public boolean createDirectory(String pathName) throws IOException {
return ftpClient.makeDirectory(pathName);
}
public boolean removeDirectory(String path) throws IOException {
return ftpClient.removeDirectory(path);
}

/**
* 删除
* @param path
* @param isAll
* @return
* @throws IOException
*/
public boolean removeDirectory(String path, boolean isAll)
throws IOException {

if (!isAll) {
return removeDirectory(path);
}

FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr == null || ftpFileArr.length == 0) {
return removeDirectory(path);
}
for (FTPFile ftpFile : ftpFileArr) {
String name = ftpFile.getName();
if (ftpFile.isDirectory()) {
System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");
removeDirectory(path + "/" + name, true);
} else if (ftpFile.isFile()) {
System.out.println("* [sF]Delete file ["+path + "/" + name+"]");
deleteFile(path + "/" + name);
} else if (ftpFile.isSymbolicLink()) {

} else if (ftpFile.isUnknown()) {

}
}
return ftpClient.removeDirectory(path);
}

/**
* 判断路径
* @param path
* @return
* @throws IOException
*/
public boolean existDirectory(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(path)) {
flag = true;
break;
}
}
return flag;
}

/**
* 获取所有文件
* @param path
* @return
* @throws IOException
*/
public List<String> getFileList(String path) throws IOException {
// listFiles return contains directory and file, it's FTPFile instance
// listNames() contains directory, so using following to filer directory.
//String[] fileNameArr = ftpClient.listNames(path);
FTPFile[] ftpFiles= ftpClient.listFiles(path);

List<String> retList = new ArrayList<String>();
if (ftpFiles == null || ftpFiles.length == 0) {
return retList;
}
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isFile()) {
retList.add(ftpFile.getName());
}
}
return retList;
}

public boolean deleteFile(String pathName) throws IOException {
return ftpClient.deleteFile(pathName);
}

/**
* 上传文件
* @param fileName
* @param newName
* @return
* @throws IOException
*/
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
/**
* 上传文件
* @param fileName
* @return
* @throws IOException
*/
public boolean uploadFile(String fileName) throws IOException {
return uploadFile(fileName, fileName);
}
/**
* 上传文件
* @param iStream
* @param newName
* @return
* @throws IOException
*/
public boolean uploadFile(InputStream iStream, String newName)
throws IOException {
boolean flag = false;
try {
// can execute [OutputStream storeFileStream(String remote)]
// Above method return's value is the local file stream.
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}

/**
* 下载文件
* @param remoteFileName
* @param localFileName
* @return
* @throws IOException
*/
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;
}

/***
* 获取文件流
* @param sourceFileName
* @return
* @throws IOException
*/
public InputStream downFile(String sourceFileName) throws IOException {
return ftpClient.retrieveFileStream(sourceFileName);
}
}

posted @ 2016-04-25 16:00  麻雀虽小五脏俱全  阅读(346)  评论(0编辑  收藏  举报