對於ftp自動上傳下載,其中還會有刪除,建立目錄等,ftp上傳是一個非常好用的常用的協議,在網上基本已經有了很多很多的內容包括工具類,在網上也看了好多前輩的文章等等,是一個非常簡單實用的東西呢,有些人的ftp上會用兩個jar包,我不知道區別在哪裡,在自己寫了一個ftpUtil之後,也就用到了一個jar,具體代碼是這樣的。
新手小白,ftp的知識點也是還有很多的,我也就只測試過上感測覺還是蠻快的,各位前輩看見那裡有什麼不如意的地方請指點。
下一篇,win7上ftp服務的搭建
jar:commons-net-3.3.jar
package com.airchina.group.utils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.SocketException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;/** * FTP Client工具類 */ public class FTPClientUtil { private static Log logger = LogFactory.getLog(FTPClient.class); private FTPClient ftpclient; // public void task(){// DateUtil.fullFormatCurrentTime();// } public FTPClientUtil(String host, int port) throws Exception { this(host, port, null, null); } public FTPClientUtil(String host, int port, String username, String password) throws Exception { ftpclient = new FTPClient(); try { ftpclient.connect(host, port); int reply = ftpclient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpclient.disconnect(); logger.fatal("FTP伺服器拒絕串連"); throw new Exception("FTP伺服器拒絕串連"); } if (username != null) { if (!ftpclient.login(username, password)) { ftpclient.disconnect(); logger.fatal("登陸驗證失敗,請檢查帳號和密碼是否正確"); throw new Exception("登陸驗證失敗,請檢查帳號和密碼是否正確"); } ftpclient.setFileType(FTP.BINARY_FILE_TYPE); } } catch (SocketException e) { logger.fatal("無法串連至指定FTP伺服器", e); throw new Exception(e); } catch (IOException e) { logger.fatal("無法用指定使用者名稱和密碼串連至指定FTP伺服器", e); throw new Exception(e); } } /** * * @param path 檔案在FTP上儲存的絕路徑 * @param input 上傳文 * @throws IOException */ public boolean upload(String pathname, InputStream input) throws IOException { // 是否是在根目錄 下 ftpclient.setFileType(FTP.BINARY_FILE_TYPE); if (pathname.indexOf("/") != -1) { String path = pathname.substring(0, pathname.lastIndexOf("/")); mkdir(path); } return ftpclient.storeFile(new String(pathname.getBytes(), ftpclient.getControlEncoding()), input); } /** * 從FTP伺服器上下載指定的檔案,命名為localName * @param pathname * @param localName * @return * @throws Exception */ public boolean download(String pathname, String localName) throws Exception { String filename = localName != null ? localName : pathname .substring(pathname.lastIndexOf("/") + 1); if (filename == null || filename.isEmpty()) { return false; } // 設定被動模式 ftpclient.enterLocalPassiveMode(); // 設定以二進位方式傳輸 ftpclient.setFileType(FTP.BINARY_FILE_TYPE); if (ftpclient.listFiles(new String(pathname.getBytes(),ftpclient.getControlEncoding())).length == 0) { logger.fatal("下載檔案不存在"); throw new Exception("下載檔案不存在"); } File tmp = new File(filename + "_tmp"); // 臨時檔案 File file = new File(filename); FileOutputStream output = null; boolean flag; try { output = new FileOutputStream(tmp); flag = ftpclient.retrieveFile(new String(pathname.getBytes(),ftpclient.getControlEncoding()), output); output.close(); if (flag) { // 下載成功,重新命名臨時檔案。 tmp.renameTo(file); System.out.println(file.getAbsolutePath()); } } catch (FileNotFoundException e) { logger.fatal("下載檔案失敗", e); throw new Exception(e); } finally { output.close(); } return flag; } /** * 只刪除檔案,如果刪除空目錄請用如下方法: <code> * getFtpclient().removeDirectory(String pathname) * </code> 參考 * {@link org.apache.commons.net.ftp.FTPClient FTPClient} * * @param pathname * @return 成功刪除返回true,否則返回false(如果檔案不存在也返回false) * @throws IOException */ public boolean delete(String pathname) throws IOException { return ftpclient.deleteFile(new String(pathname.getBytes(),ftpclient.getControlEncoding())); } /** * 改變目前的目錄至pathname,"/"代表根目錄 * * @param pathname * 路徑名 * @return 如果改變成功返true否則返回false * @throws IOException */ public boolean changeWorkingDirectory(String pathname) throws IOException { return ftpclient.changeWorkingDirectory(new String(pathname.getBytes(),ftpclient.getControlEncoding())); } /** * @return {@link org.apache.commons.net.ftp.FTPClient FTPClient}對象 */ public FTPClient getFtpclient() { return this.ftpclient; } /** * @param ftpclient * {@link org.apache.commons.net.ftp.FTPClient FTPClient}對象 */ public void setFtpclient(FTPClient ftpclient) { this.ftpclient = ftpclient; } public void close() throws Exception { ftpclient.disconnect(); } /** * * @param pathname * 要建立的目錄路徑,可以是相對路徑,也可以是絕路徑("/"開始) * @return 如果成功建立目錄返回true,否則返回false(如果目錄已存在也返回false) * @throws IOException */ public boolean mkdir(String pathname) throws IOException { try{ ftpclient.setControlEncoding("ISO-8859-1"); //注意編碼,如果不編碼檔案中文目錄無法建立 return ftpclient.makeDirectory(new String(pathname.getBytes(),ftpclient.getControlEncoding())); }catch(Exception ex){ ex.printStackTrace(); return false; } } }