要用到的jar:oro-2.0.8.jar,commons-net-1.3.0.jar,轉載後糾錯的版本
import java.io.File; 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 s { public static int port=21; public static String url="172.19.9.99"; public static String username="administrator"; public static String password="321"; /** * Description: 向FTP服務器上傳文件 * @param url 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 url, int port, String username, String password, String path, String filename, InputStream input) { boolean success = false; FTPClient ftp = new FTPClient(); ftp.setControlEncoding("GBK"); try { int reply; // 連接FTP服務器 if (port > -1) { ftp.connect(url, port); } else { ftp.connect(url); } // 登錄FTP ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.setFileType(FTP.BINARY_FILE_TYPE); 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; } /** * Description: 從FTP服務器下載文件 * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔紅保([email protected])創建 * @param url FTP服務器hostname * @param port FTP服務器端口 * @param username FTP登錄賬號 * @param password FTP登錄密碼 * @param remotePath FTP服務器上的相對路徑 * @param fileName 要下載的文件名 * @param localPath 下載後保存到本地的路徑 * @return */ public static boolean downloadFile(String url, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean success = false; FTPClient ftp = new FTPClient(); ftp.setControlEncoding("GBK"); try { int reply; // 連接FTP服務器 if (port > -1) { ftp.connect(url, port); } else { ftp.connect(url); } 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 f=new File(localPath); if(!f.exists()){ f.mkdir(); } 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; } /** * <刪除FTP上的文件> * <遠程刪除FTP服務器上的錄音文件> * @param url FTP服務器IP地址 * @param port FTP服務器端口 * @param username FTP服務器登錄名 * @param password FTP服務器密碼 * @param remotePath 遠程文件路徑 * @param fileName 待刪除的文件名 * @return * @see [類、類#方法、類#成員] */ public static boolean deleteFtpFile(String url, int port, String username, String password, String remotePath, String fileName) { boolean success = false; FTPClient ftp = new FTPClient(); ftp.setControlEncoding("GBK"); try { int reply; // 連接FTP服務器 if (port > -1) { ftp.connect(url, port); } else { ftp.connect(url); } // 登錄 ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } // 轉移到FTP服務器目錄 ftp.changeWorkingDirectory(remotePath); success = ftp.deleteFile( fileName); ftp.logout(); } catch (IOException e) { e.printStackTrace(); success = false; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return success; } public static void main(String []args){ //downloadFile(url, port, username, password, "data/innovate/", "1.jpg", "d:/1/"); // boolean s=deleteFtpFile(url, port, username, password, "data/innovate/", "95598服務情況.jpg"); // System.out.println(s); // File f = new File("d:/instPlug4MyEclipse.jar"); // InputStream input; // try { // input = new FileInputStream(f); // boolean x=uploadFile(url, port, username, password, "data/innovate/", "instPlug4MyEclipse.jar", input); // System.out.println(x); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } }