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 org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* @author xuyang 导入commons-net-1.4.1.jar、jakarta-oro-2.0.8.jar这两个包
* 文件、文件夹的名称有中文,先转换字符集
* 如:new String(fileName.getBytes("gb2312"),"iso8859-1");
*/
public class FtpUtil {
private FTPClient ftp;
/**
* 连接FTP服务器
*
* @param serverIP
* @param user
* @param password
* @return boolean
*/
public boolean connectFtpServer(String serverIP, String user,
String password) {
ftp = new FTPClient();
int reply;
try {
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
return false;
}
if (!ftp.login(user, password)) {
disconnect();
return false;
}
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 创建文件夹
*
* @param path
* @return boolean
*/
public boolean createDirectory(String path) {
try {
String[] dirArray = path.split("/");
for (int i = 0; i < dirArray.length; i++) {
if (dirArray[i] != null)
if (!ftp.changeWorkingDirectory(dirArray[i])) {
ftp.makeDirectory(dirArray[i]);
ftp.changeWorkingDirectory(dirArray[i]);
}
}
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 下载文件
*
* @param local
* @param remote
* @return boolean
*/
public boolean downloadFile(String local, String remote) {
OutputStream output = null;
try {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
String[] dirArray = remote.split("/");
if (dirArray.length > 1) {
for (int i = 0; i < dirArray.length - 1; i++) {
if (dirArray[i] != null) {
ftp.changeWorkingDirectory(dirArray[i]);
}
}
}
File file = new File(local);
output = new FileOutputStream(file);
ftp.retrieveFile(dirArray[dirArray.length - 1], output);
output.close();
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 上传文件
*
* @param local
* @param remote
* @return boolean
*/
public boolean uploadFile(String local, String remote) {
InputStream input = null;
try {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
String[] dirArray = remote.split("/");
if (dirArray.length > 1) {
for (int i = 0; i < dirArray.length - 1; i++) {
if (dirArray[i] != null) {
ftp.changeWorkingDirectory(dirArray[i]);
}
}
}
input = new FileInputStream(local);
ftp.storeFile(dirArray[dirArray.length - 1], input);
input.close();
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 删除文件
*
* @param filePath
* @return boolean
*/
public boolean deleteFile(String filePath) {
try {
String[] dirArray = filePath.split("/");
if (dirArray.length > 1) {
for (int i = 0; i < dirArray.length - 1; i++) {
if (dirArray[i] != null) {
ftp.changeWorkingDirectory(dirArray[i]);
}
}
}
ftp.deleteFile(dirArray[dirArray.length - 1]);
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 删除文件夹
*
* @param path
* @return boolean
*/
public boolean removeDirectory(String path) {
try {
String[] dirArray = path.split("/");
for (int i = 0; i < dirArray.length; i++) {
if (dirArray[i] != null) {
ftp.changeWorkingDirectory(dirArray[i]);
}
}
FTPFile[] files = ftp.listFiles();
for (int j = 0; j < files.length; j++) {
if (files[j].isDirectory()) { // 递归删除子文件夹
removeDirectory(files[j].getName());
}
ftp.deleteFile(files[j].getName());
}
ftp.changeToParentDirectory();
ftp.removeDirectory(dirArray[dirArray.length - 1]);
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 重命名文件、文件夹
*
* @param from
* @param to
* @return boolean
*/
public boolean rename(String from, String to) {
try {
String[] dirArray = from.split("/");
if (dirArray.length > 1) {
for (int i = 0; i < dirArray.length - 1; i++) {
if (dirArray[i] != null) {
ftp.changeWorkingDirectory(dirArray[i]);
}
}
}
ftp.rename(dirArray[dirArray.length - 1], to);
} catch (IOException e) {
disconnect();
return false;
}
return true;
}
/**
* 断开连接
*/
public void disconnect() {
if (ftp.isConnected()) {
try {
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}