package common.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
/**
* 列出FTP服務器上指定目錄下面的所有文件
*/
public class FTPListAllFiles {
private static Logger logger = Logger.getLogger(FTPListAllFiles.class);
public FTPClient ftp;
public ArrayList<String> arFiles;
private static final String FAR_SERVER_URL="***.***.***.***";
private static final int SERVER_PORT=21;
private static final String SERVER_USER="***";
private static final String SERVER_PWD="***";
private static final String path="/123/456/";
private static final String showPath="";
/**
* 登陸FTP服務器
* @param host FTPServer IP地址
* @param port FTPServer 端口
* @param username FTPServer 登陸用戶名
* @param password FTPServer 登陸密碼
* @return 是否登錄成功
* @throws IOException
*/
public boolean login(String host,int port,String username,String password) throws IOException{
this.ftp.connect(host,port);
if(FTPReply.isPositiveCompletion(this.ftp.getReplyCode())){
if(this.ftp.login(username, password)){
this.ftp.setControlEncoding("GBK");
return true;
}
}
if(this.ftp.isConnected()){
this.ftp.disconnect();
}
return false;
}
/**
* 關閉數據鏈接
* @throws IOException
*/
public void disConnection() throws IOException{
if(this.ftp.isConnected()){
this.ftp.disconnect();
}
}
/**
* 遞歸遍歷出目錄下面所有文件
* @param pathName 需要遍歷的目錄,必須以"/"開始和結束
* @throws IOException
*/
public String List(String pathName) throws IOException{
StringBuffer filename=new StringBuffer();
if(pathName.startsWith("/")&&pathName.endsWith("/")){
String directory = pathName;
//更換目錄到當前目錄
this.ftp.changeWorkingDirectory(directory);
ftp.enterLocalPassiveMode();
FTPFile[] files = this.ftp.listFiles();
if(files!=null){
for (int i = 0; i < files.length; i++) {
if(files[i].isFile()){
String n=new String(files[i].getName().getBytes("gbk"),"utf-8");
if(i==files.length-1){
filename.append(n+","+showPath);
}else{
filename.append(n+",");
}
}
}
}
}
return filename.toString();
}
//獲取指定文件夾內的文件名稱
public static String getFilenames(){
String names="";
FTPListAllFiles f = new FTPListAllFiles();
try {
if(f.login(FAR_SERVER_URL, SERVER_PORT, SERVER_USER, SERVER_PWD)){
names= f.List(path);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
f.disConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return names;
}
//測試
/* public static void main(String[] args) throws IOException {
System.out.println(getFilenames());
} */
}
注意:紅色標記的代碼很重要,如果Linux搭建的ftp環境沒有開啟被動模式傳輸數據時,一般不會讀取到指定文件夾內的文件名稱。
經過在網上查找相關的知識,該語句的作用為:
調用FTPClient.enterLocalPassiveMode();這個方法的意思就是每次數據連接之前,ftp client告訴ftp server開通一個端口來傳輸數據。為什么要這樣做呢,因為ftp server可能每次開啟不同的端口來傳輸數據,但是在linux上,由於安全限制,可能某些端口沒有開啟,所以就出現阻塞。
