|
主要用到两个包:
commons-net-1.4.1.jar jakarta-oro-2.0.8.jar
一、
package tmp.ftp;
/** * FTP实体Bean * * @author zhanjia * */ public class FtpBean { // ftpHost private java.lang.String ftpHost;
// ftpPort private java.lang.String ftpPort;
// ftpUser private java.lang.String ftpUser;
// ftpPassword private java.lang.String ftpPassword;
// ftpPath private java.lang.String ftpPath;
......
二、FTP测试类
/** * */ package tmp.ftp;
import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply;
import examples.PrintCommandListener;
/** * FTP测试类 * * @author zhanjia * */ public class TestFtp {
private static final Log log = LogFactory.getLog(TestFtp.class);
/** * 测试连接FTP服务器是否成功 * * @param ftpBean * @throws Exception */ public static void connectFTP(FtpBean ftpBean) { FTPClient ftp = new FTPClient(); PrintCommandListener listener = new PrintCommandListener( new PrintWriter(System.out)); ftp.addProtocolCommandListener(listener); ftp.setDefaultPort(Integer.parseInt(ftpBean.getFtpPort())); // 设置默认端口
//boolean error = false; try { int reply; ftp.connect(ftpBean.getFtpHost()); // 连接主机 System.out.println("Connected to " + ftpBean.getFtpHost() + "."); // System.out.print(ftp.getReplyString());
// 尝试连接后, 检查返回码验证是否连接成功 reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.err.println("FTP server refused connection."); System.exit(1); } System.out.println("连接成功!"); } catch (IOException e) { if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } System.err.println("连接服务器失败."); e.printStackTrace(); System.exit(1); } finally { if (ftp != null && ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { // do nothing } } } }
/** * 获取FTP根目录列表 * * @param ftpBean * @return * @throws Exception */ public static Map getFTPPathList(FtpBean ftpBean) { Map paths = null; String fName = null; String userName = ftpBean.getFtpUser(); String password = ftpBean.getFtpPassword(); /* * if (userName.equals("anonymous")) { password = "anonymous"; } */
FTPClient ftp = new FTPClient(); PrintCommandListener listener = new PrintCommandListener( new PrintWriter(System.out)); ftp.addProtocolCommandListener(listener); ftp.setControlEncoding("GBK"); // 设置编码 ftp.setDefaultPort(Integer.parseInt(ftpBean.getFtpPort())); // 设置默认端口
try { ftp.connect(ftpBean.getFtpHost()); // 连接主机 System.out.println("连接到 " + ftpBean.getFtpHost() + "."); if (ftp.login(userName, password)) { // 登录 System.out.println("登录成功!"); System.out.println("FTP目录: " + ftp.printWorkingDirectory()); } else { System.out.println("登录失败!"); }
// ftp.type(FTPClient.ASCII_FILE_TYPE); FTPFile files[] = ftp.listFiles(); if (files != null) { int a = 0, b = 0; paths = new HashMap(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { fName = files[i].getName(); if (!".".equals(fName) && !"..".equals(fName)) { paths.put("目录" + (a++), fName); } } else if (files[i].isFile()) { fName = files[i].getName(); if (!"<DIR>".equalsIgnoreCase(fName)) { paths.put("文件" + (b++), files[i].getName()); } } } } } catch (IOException e) { if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } System.err.println("连接服务器失败."); e.printStackTrace(); System.exit(1); } finally { if (ftp != null && ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { // do nothing } } } return paths; }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { FtpBean ftpBean = new FtpBean(); ftpBean.setFtpHost("61.235.112.241"); ftpBean.setFtpPort("21"); //ftpBean.setFtpUser("anonymous11"); ftpBean.setFtpUser("anonymous"); ftpBean.setFtpPassword("anonymous");
System.out.println("-- 测试连接 --"); TestFtp.connectFTP(ftpBean);
System.out.println(); System.out.println("-- 获取FTP根目录列表 --"); Map paths = TestFtp.getFTPPathList(ftpBean); System.out.println(); System.out.println("-- 文件与目录 --"); if (paths != null) { Iterator it = paths.entrySet().iterator(); while (it.hasNext()) { Entry entry = (Entry) it.next(); String name = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println(name + ": " + value); } }
}
}
个人博客: http://zhan.zhmy.com
|