使用java commons net包中的api可以方便操作ftp操作。
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.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FtpCtrl { public static void main(String[] args) throws Exception { String url = "ip"; int port = 21; String username = "username"; String password = "password"; String path = "/"; String filename = "ftp.test.tmp"; FileInputStream fis = new FileInputStream("/home/iamsuperman/ftp.txt"); uploadFile(url, port, username, password, path, filename, fis); } /** * @param ip * FTP服务器IP地址 * @param port * FTP服务器端口 * @param username * FTP服务器登录名 * @param password * FTP服务器密码 * @param remotePath * 远程文件路径 * @param fileName * 待删除的文件名 * @return * @see [类、类#方法、类#成员] */ public static boolean deleteFtpFile(String ip, int port, String username, String password, String remotePath, String fileName) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; // 连接FTP服务器 if (port > -1) { ftp.connect(ip, port); } else { ftp.connect(ip); } // 登录 ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } // 转移到FTP服务器目录 ftp.changeWorkingDirectory(remotePath); success = ftp.deleteFile(remotePath + "/" + fileName); ftp.logout(); } catch (IOException e) { e.printStackTrace(); success = false; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return success; } /** * Description: 从FTP服务器下载文件 * * @param ip * FTP服务器hostname * @param port * FTP服务器端口 * @param username * FTP登录账号 * @param password * FTP登录密码 * @param remotePath * FTP服务器上的相对路径 * @param fileName * 要下载的文件名 * @param localPath * 下载后保存到本地的路径 * @return */ public static boolean downloadFile(String ip, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; // 连接FTP服务器 if (port > -1) { ftp.connect(ip, port); } else { ftp.connect(ip); } ftp.login(username, password);// 登录 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return success; } /** * Description: 向FTP服务器上传文件 * * @param ip * FTP服务器hostname * @param port * FTP服务器端口,如果默认端口请写-1 * @param username * FTP登录账号 * @param password * FTP登录密码 * @param path * FTP服务器保存目录 * @param filename * 上传到FTP服务器上的文件名 * @param input * 输入流 * @return 成功返回true,否则返回false */ public static boolean uploadFile(String ip, int port, String username, String password, String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; // 连接FTP服务器 if (port > -1) { ftp.connect(ip, port); } else { ftp.connect(ip); } // 登录FTP ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(path); ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { success = false; e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return success; } }
==============================================================================
获取连接时候:
ftp.enterLocalPassiveMode();
这句话特别重要。