package com.tobacco.ermsuite.interfaces.util;
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.util.ArrayList;
import java.util.List;
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;
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;
/**
* @param server
* ftp服务器地址(IP)
* @param username
* ftp服务器登陆用户
* @param password
* @return 是否连接成功
*/
public boolean connectServer(String servername, int port, String username,
String password) {
boolean result = false;
ftpClient = new FTPClient();
try {
ftpClient.connect(servername, port);
ftpClient.setControlEncoding("GBK");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
result = true;
}
}
} catch (Exception e) {
disconnect();
e.printStackTrace();
}
return result;
}
/**
* @param server
* ftp服务器地址
* @param username
* ftp服务器登陆用户
* @param password
*
*/
public boolean connectServer(String servername, int port, String username,
String password, String path) {
boolean result = false;
result = connectServer(servername, port, username, password);
if (path.length() != 0 && result) {
try {
changeDirectory(path);
} catch (IOException e) {
result = false;// 设置目录失败
e.printStackTrace();
}
}
return result;
}
/**
* 断开与远程服务器的连接
*/
public void disconnect() {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void setFileType(int fileType) throws IOException {
ftpClient.setFileType(fileType);
}
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;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
public boolean uploadFile(String fileName) throws IOException {
return uploadFile(fileName, fileName);
}
public boolean uploadFile(InputStream iStream, String newName)
throws IOException {
boolean flag = false;
try {
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
} 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;
} finally {
oStream.close();
}
return flag;
}
public InputStream downFile(String sourceFileName) throws IOException {
return ftpClient.retrieveFileStream(sourceFileName);
}
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);
}
// delete all subDirectory and files.
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);
}
// Check the path is exist; exist return true, else false.
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;
}
public List<String> getFileList(String path) throws IOException {
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);
}
}