import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 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 Client * * @author cuiyh * @version $Revision: 1.1 $ $Date: 2006/06/02 02:50:54 $ */ public class FtpClient { private String server; private String username; private String password; private FTPClient ftp; private boolean binaryTransfer = false; private final static Logger log = Logger.getLogger(FtpClient.class); /** * @param server * ftp服务器地址 * @param username * ftp服务器登陆用户 * @param password * ftp用户密码 */ public FtpClient(String server, String username, String password) { this.server = server; this.username = username; this.password = password; ftp = new FTPClient(); /* * if(Configuration.PrintFTPCommandLog){ //打印FTP命令 * ftp.addProtocolCommandListener(new PrintCommandListener()); } */ } /** * 根据配置文件构建一个FtpClient */ public FtpClient() { this(Configuration.FtpServer, Configuration.FtpUser, Configuration.FtpPassword); } public boolean connect() { try { int reply; ftp.connect(server); // 连接后检测返回码来校验连接是否成功 reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (ftp.login(username, password)) { // 设置为passive模式 ftp.enterLocalPassiveMode(); return true; } } else { ftp.disconnect(); log.error("FTP server refused connection."); } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { } } log.error("Could not connect to server.", e); } return false; } /** * 下载一个文件到默认的本地路径中 * * @param fileName * 文件名称(不含路径) * @param delFile * 成功后是否删除该文件 * @return */ public boolean get(String fileName, boolean delFile) { String remote = Configuration.RemoteDownPath + fileName; String local = Configuration.LocalDownPath + fileName; return get(remote, local, delFile); } /** * 上传一个文件到默认的远程路径中 * * @param fileName * 文件名称(不含路径) * @param delFile * 成功后是否删除该文件 * @return */ public boolean put(String fileName, boolean delFile) { String remote = Configuration.RemoteUpPath + fileName; String local = Configuration.LocalUpPath + fileName; return put(remote, local, delFile); } /** * 上传多个文件到默认的远程路径中 * * @param fileNames * 文件名数组 * @param delFile * 成功后是否删除文件 * @return */ public boolean[] put(String[] fileNames, boolean delFile) { boolean[] result = new boolean[fileNames.length]; for (int j = 0; j < result.length; j++) { result[j] = false; } String remoteFile; String localFile; for (int i = 0; i < fileNames.length; i++) { localFile = fileNames[i]; result[i] = put(localFile, delFile); } return result; } /** * 上传一个本地文件到远程指定文件 * * @param remoteAbsoluteFile * 远程文件名(包括完整路径) * @param localAbsoluteFile * 本地文件名(包括完整路径) * @return 成功时,返回true,失败返回false */ private boolean put(String remoteAbsoluteFile, String localAbsoluteFile, boolean delFile) { InputStream input = null; try { // //设置文件传输类型 if (binaryTransfer) { ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } else { ftp.setFileType(FTPClient.ASCII_FILE_TYPE); } // 处理传输 input = new FileInputStream(localAbsoluteFile); ftp.storeFile(remoteAbsoluteFile, input); log.debug("put " + localAbsoluteFile); input.close(); if (delFile) { (new File(localAbsoluteFile)).delete(); } log.debug("delete " + localAbsoluteFile); return true; } catch (FileNotFoundException e) { log.error("local file not found.", e); } catch (IOException e1) { log.error("Could put file to server.", e1); } finally { try { if (input != null) { input.close(); } } catch (Exception e2) { } } return false; } /** * 下载一个远程文件到本地的指定文件 * * @param remoteAbsoluteFile * 远程文件名(包括完整路径) * @param localAbsoluteFile * 本地文件名(包括完整路径) * @return 成功时,返回true,失败返回false */ public boolean get(String remoteAbsoluteFile, String localAbsoluteFile, boolean delFile) { OutputStream output = null; try { // 设置文件传输类型 if (binaryTransfer) { ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } else { ftp.setFileType(FTPClient.ASCII_FILE_TYPE); } // 处理传输 output = new FileOutputStream(localAbsoluteFile); ftp.retrieveFile(remoteAbsoluteFile, output); output.close(); if (delFile) { // 删除远程文件 ftp.deleteFile(remoteAbsoluteFile); } return true; } catch (FileNotFoundException e) { log.error("local file not found.", e); } catch (IOException e1) { log.error("Could get file from server.", e1); } finally { try { if (output != null) { output.close(); } } catch (IOException e2) { } } return false; } /** * 列出远程目录下所有的文件 * * @param remotePath * 远程目录名 * @return 远程目录下所有文件名的列表,目录不存在或者目录下没有文件时返回0长度的数组 */ public String[] listNames(String remotePath) { String[] fileNames = null; try { FTPFile[] remotefiles = ftp.listFiles(remotePath); fileNames = new String[remotefiles.length]; for (int i = 0; i < remotefiles.length; i++) { fileNames[i] = remotefiles[i].getName(); } } catch (IOException e) { log.error("Could not list file from server.", e); } return fileNames; } /** * 断开ftp连接 */ public void disconnect() { try { ftp.logout(); if (ftp.isConnected()) { ftp.disconnect(); } } catch (IOException e) { log.error("Could not disconnect from server.", e); } } /** * @return Returns the binaryTransfer. */ public boolean isBinaryTransfer() { return binaryTransfer; } /** * @param binaryTransfer * The binaryTransfer to set. */ public void setBinaryTransfer(boolean binaryTransfer) { this.binaryTransfer = binaryTransfer; } public static void main(String[] args) { FtpClient ftp = new FtpClient("130.85.51.131", "cbs", "cbs"); ftp.connect(); String[] temp = ftp.listNames("/tuxlog/cbs"); System.out.println("connect sucess"); System.out.println(temp.length); // ftp.put("test.txt", true); // System.out.println("upload sucess"); /* * boolean re = ftp.get("/expert/acc/bill/src/test.txt", * "c://test02.txt", false); * * if (re) { System.out.println("down sucess"); } else { * System.out.println("down sucess"); } */ /* * if(ftp.get("test.txt",false)){ System.out.println("down sucess"); * }else{ System.out.println("down faile"); } */ ftp.disconnect(); } }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 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 Client * * @author cuiyh * @version $Revision: 1.1 $ $Date: 2006/06/02 02:50:54 $ */ public class FtpClient { private String server; private String username; private String password; private FTPClient ftp; private boolean binaryTransfer = false; private final static Logger log = Logger.getLogger(FtpClient.class); /** * @param server * ftp服务器地址 * @param username * ftp服务器登陆用户 * @param password * ftp用户密码 */ public FtpClient(String server, String username, String password) { this.server = server; this.username = username; this.password = password; ftp = new FTPClient(); /* * if(Configuration.PrintFTPCommandLog){ //打印FTP命令 * ftp.addProtocolCommandListener(new PrintCommandListener()); } */ } /** * 根据配置文件构建一个FtpClient */ public FtpClient() { this(Configuration.FtpServer, Configuration.FtpUser, Configuration.FtpPassword); } public boolean connect() { try { int reply; ftp.connect(server); // 连接后检测返回码来校验连接是否成功 reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { if (ftp.login(username, password)) { // 设置为passive模式 ftp.enterLocalPassiveMode(); return true; } } else { ftp.disconnect(); log.error("FTP server refused connection."); } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { } } log.error("Could not connect to server.", e); } return false; } /** * 下载一个文件到默认的本地路径中 * * @param fileName * 文件名称(不含路径) * @param delFile * 成功后是否删除该文件 * @return */ public boolean get(String fileName, boolean delFile) { String remote = Configuration.RemoteDownPath + fileName; String local = Configuration.LocalDownPath + fileName; return get(remote, local, delFile); } /** * 上传一个文件到默认的远程路径中 * * @param fileName * 文件名称(不含路径) * @param delFile * 成功后是否删除该文件 * @return */ public boolean put(String fileName, boolean delFile) { String remote = Configuration.RemoteUpPath + fileName; String local = Configuration.LocalUpPath + fileName; return put(remote, local, delFile); } /** * 上传多个文件到默认的远程路径中 * * @param fileNames * 文件名数组 * @param delFile * 成功后是否删除文件 * @return */ public boolean[] put(String[] fileNames, boolean delFile) { boolean[] result = new boolean[fileNames.length]; for (int j = 0; j < result.length; j++) { result[j] = false; } String remoteFile; String localFile; for (int i = 0; i < fileNames.length; i++) { localFile = fileNames[i]; result[i] = put(localFile, delFile); } return result; } /** * 上传一个本地文件到远程指定文件 * * @param remoteAbsoluteFile * 远程文件名(包括完整路径) * @param localAbsoluteFile * 本地文件名(包括完整路径) * @return 成功时,返回true,失败返回false */ private boolean put(String remoteAbsoluteFile, String localAbsoluteFile, boolean delFile) { InputStream input = null; try { // //设置文件传输类型 if (binaryTransfer) { ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } else { ftp.setFileType(FTPClient.ASCII_FILE_TYPE); } // 处理传输 input = new FileInputStream(localAbsoluteFile); ftp.storeFile(remoteAbsoluteFile, input); log.debug("put " + localAbsoluteFile); input.close(); if (delFile) { (new File(localAbsoluteFile)).delete(); } log.debug("delete " + localAbsoluteFile); return true; } catch (FileNotFoundException e) { log.error("local file not found.", e); } catch (IOException e1) { log.error("Could put file to server.", e1); } finally { try { if (input != null) { input.close(); } } catch (Exception e2) { } } return false; } /** * 下载一个远程文件到本地的指定文件 * * @param remoteAbsoluteFile * 远程文件名(包括完整路径) * @param localAbsoluteFile * 本地文件名(包括完整路径) * @return 成功时,返回true,失败返回false */ public boolean get(String remoteAbsoluteFile, String localAbsoluteFile, boolean delFile) { OutputStream output = null; try { // 设置文件传输类型 if (binaryTransfer) { ftp.setFileType(FTPClient.BINARY_FILE_TYPE); } else { ftp.setFileType(FTPClient.ASCII_FILE_TYPE); } // 处理传输 output = new FileOutputStream(localAbsoluteFile); ftp.retrieveFile(remoteAbsoluteFile, output); output.close(); if (delFile) { // 删除远程文件 ftp.deleteFile(remoteAbsoluteFile); } return true; } catch (FileNotFoundException e) { log.error("local file not found.", e); } catch (IOException e1) { log.error("Could get file from server.", e1); } finally { try { if (output != null) { output.close(); } } catch (IOException e2) { } } return false; } /** * 列出远程目录下所有的文件 * * @param remotePath * 远程目录名 * @return 远程目录下所有文件名的列表,目录不存在或者目录下没有文件时返回0长度的数组 */ public String[] listNames(String remotePath) { String[] fileNames = null; try { FTPFile[] remotefiles = ftp.listFiles(remotePath); fileNames = new String[remotefiles.length]; for (int i = 0; i < remotefiles.length; i++) { fileNames[i] = remotefiles[i].getName(); } } catch (IOException e) { log.error("Could not list file from server.", e); } return fileNames; } /** * 断开ftp连接 */ public void disconnect() { try { ftp.logout(); if (ftp.isConnected()) { ftp.disconnect(); } } catch (IOException e) { log.error("Could not disconnect from server.", e); } } /** * @return Returns the binaryTransfer. */ public boolean isBinaryTransfer() { return binaryTransfer; } /** * @param binaryTransfer * The binaryTransfer to set. */ public void setBinaryTransfer(boolean binaryTransfer) { this.binaryTransfer = binaryTransfer; } public static void main(String[] args) { FtpClient ftp = new FtpClient("130.85.51.131", "cbs", "cbs"); ftp.connect(); String[] temp = ftp.listNames("/tuxlog/cbs"); System.out.println("connect sucess"); System.out.println(temp.length); // ftp.put("test.txt", true); // System.out.println("upload sucess"); /* * boolean re = ftp.get("/expert/acc/bill/src/test.txt", * "c://test02.txt", false); * * if (re) { System.out.println("down sucess"); } else { * System.out.println("down sucess"); } */ /* * if(ftp.get("test.txt",false)){ System.out.println("down sucess"); * }else{ System.out.println("down faile"); } */ ftp.disconnect(); } }