最近在項目中須要和ftp服務器進行交互,在網上找了一下關於ftp上傳下載的工具類,大體有兩種。java
第一種是單例模式的類。spring
第二種是另外定義一個Service,直接經過Service來實現ftp的上傳下載刪除。apache
這兩種感受都有利弊。json
第一種實現了代碼複用,可是配置信息全須要寫在類中,維護比較複雜。服務器
第二種若是是spring框架,能夠經過propertis文件,動態的注入配置信息,可是又不能代碼複用。app
因此我打算本身實現一個工具類,來把上面的兩種優勢進行整合。順便把一些上傳過程當中一些常見的問題也給解決了。框架
由於我使用的是spring框架,若是把工具類聲明爲bean給spring管理,他默認就是單例的,因此不須要我再實現單例。而且由於是bean,因此我能夠把properties文件的屬性注入bean的屬性中,實現解耦,下面是具體代碼:ide
package com.cky.util; 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.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; //使用spring自動生成單例對象, //@Component public class FtpUtil { //經過properties文件自動注入 @Value("${ftp.host}") private String host; //ftp服務器ip @Value("${ftp.port}") private int port; //ftp服務器端口 @Value("${ftp.username}") private String username;//用戶名 @Value("${ftp.password}") private String password;//密碼 @Value("${ftp.basePath}") private String basePath;//存放文件的基本路徑 //測試的時候把這個構造函數打開,設置你的初始值,而後在代碼後面的main方法運行測試 public FtpUtil() { //System.out.println(this.toString()); host="192.168.100.77"; port=21; username="ftpuser"; password="ftp54321"; basePath="/home/ftpuser/www/images"; } /** * * @param path 上傳文件存放在服務器的路徑 * @param filename 上傳文件名 * @param input 輸入流 * @return */ public boolean fileUpload(String path,String filename,InputStream input) { FTPClient ftp=new FTPClient(); try { ftp.connect(host, port); ftp.login(username, password); //設置文件編碼格式 ftp.setControlEncoding("UTF-8"); //ftp通訊有兩種模式 //PORT(主動模式)客戶端開通一個新端口(>1024)並經過這個端口發送命令或傳輸數據,期間服務端只使用他開通的一個端口,例如21 //PASV(被動模式)客戶端向服務端發送一個PASV命令,服務端開啓一個新端口(>1024),並使用這個端口與客戶端的21端口傳輸數據 //因爲客戶端不可控,防火牆等緣由,因此須要由服務端開啓端口,須要設置被動模式 ftp.enterLocalPassiveMode(); //設置傳輸方式爲流方式 ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); //獲取狀態碼,判斷是否鏈接成功 if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new RuntimeException("FTP服務器拒絕鏈接"); } //轉到上傳文件的根目錄 if(!ftp.changeWorkingDirectory(basePath)) { throw new RuntimeException("根目錄不存在,須要建立"); } //判斷是否存在目錄 if(!ftp.changeWorkingDirectory(path)) { String[] dirs=path.split("/"); //建立目錄 for (String dir : dirs) { if(null==dir||"".equals(dir)) continue; //判斷是否存在目錄 if(!ftp.changeWorkingDirectory(dir)) { //不存在則建立 if(!ftp.makeDirectory(dir)) { throw new RuntimeException("子目錄建立失敗"); } //進入新建立的目錄 ftp.changeWorkingDirectory(dir); } } //設置上傳文件的類型爲二進制類型 ftp.setFileType(FTP.BINARY_FILE_TYPE); //上傳文件 if(!ftp.storeFile(filename, input)) { return false; } input.close(); ftp.logout(); return true; } } catch (Exception e) { throw new RuntimeException(e); }finally { if(ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { throw new RuntimeException(e); } } } return false; } /** * * @param filename 文件名,注意!此處文件名爲加路徑文件名,如:/2015/06/04/aa.jpg * @param localPath 存放到本地第地址 * @return */ public boolean downloadFile(String filename,String localPath) { FTPClient ftp=new FTPClient(); try { ftp.connect(host, port); ftp.login(username, password); //設置文件編碼格式 ftp.setControlEncoding("UTF-8"); //ftp通訊有兩種模式 //PORT(主動模式)客戶端開通一個新端口(>1024)並經過這個端口發送命令或傳輸數據,期間服務端只使用他開通的一個端口,例如21 //PASV(被動模式)客戶端向服務端發送一個PASV命令,服務端開啓一個新端口(>1024),並使用這個端口與客戶端的21端口傳輸數據 //因爲客戶端不可控,防火牆等緣由,因此須要由服務端開啓端口,須要設置被動模式 ftp.enterLocalPassiveMode(); //設置傳輸方式爲流方式 ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); //獲取狀態碼,判斷是否鏈接成功 if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new RuntimeException("FTP服務器拒絕鏈接"); } int index=filename.lastIndexOf("/"); //獲取文件的路徑 String path=filename.substring(0, index); //獲取文件名 String name=filename.substring(index+1); //判斷是否存在目錄 if(!ftp.changeWorkingDirectory(basePath+path)) { throw new RuntimeException("文件路徑不存在:"+basePath+path); } //獲取該目錄全部文件 FTPFile[] files=ftp.listFiles(); for (FTPFile file : files) { //判斷是否有目標文件 //System.out.println("文件名"+file.getName()+"---"+name); if(file.getName().equals(name)) { //System.out.println("找到文件"); //若是找到,將目標文件複製到本地 File localFile =new File(localPath+"/"+file.getName()); OutputStream out=new FileOutputStream(localFile); ftp.retrieveFile(file.getName(), out); out.close(); } } ftp.logout(); return true; } catch (Exception e) { throw new RuntimeException(e); }finally { if(ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { throw new RuntimeException(e); } } } } public boolean deleteFile(String filename) { FTPClient ftp=new FTPClient(); try { ftp.connect(host, port); ftp.login(username, password); //設置編碼格式 ftp.setControlEncoding("UTF-8"); ftp.enterLocalPassiveMode(); //獲取狀態碼,判斷是否鏈接成功 if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new RuntimeException("FTP服務器拒絕鏈接"); } int index=filename.lastIndexOf("/"); //獲取文件的路徑 String path=filename.substring(0, index); //獲取文件名 String name=filename.substring(index+1); //判斷是否存在目錄,不存在則說明文件存在 if(!ftp.changeWorkingDirectory(basePath+path)) { return true; } if(ftp.deleteFile(name)) { clearDirectory(ftp, basePath, path); ftp.logout(); return true; } ftp.logout(); return false; } catch (Exception e) { throw new RuntimeException(e); }finally { if(ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { throw new RuntimeException(e); } } } } /** * * @param ftp * @param basePath * @param path 以path爲根,遞歸清除上面全部空的文件夾,直到出現不爲空的文件夾中止,最多清除到basePath結束 * @throws IOException */ private void clearDirectory(FTPClient ftp,String basePath,String path) throws IOException { //若是路徑長度小於2,說明到頂了 if(path.length()<2) { return ; } //若是當前目錄文件數目小於1則刪除此目錄 if(ftp.listNames(basePath+path).length<1) { //刪除目錄 System.out.println("刪除目錄:"+basePath+path); ftp.removeDirectory(basePath+path); int index=path.lastIndexOf("/"); //路徑向上一層 path=path.substring(0, index); //繼續判斷 clearDirectory(ftp, basePath, path); } } //兩個功能其中一個使用的話另外一個須要註釋 public static void main(String []args) { //上傳測試-------------------------------------- /*FileInputStream in; try { in=new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\json.png")); FtpUtil ftputil=new FtpUtil(); boolean flag=ftputil.fileUpload("/2015/06/04", "va.jpg", in); System.out.println(flag); }catch (Exception e) { e.printStackTrace(); }finally { }*/ //下載測試-------------------------------------- /*String filename="/2015/06/04/aa.jpg"; String localPath="F:\\"; FtpUtil ftputil=new FtpUtil(); ftputil.downloadFile(filename, localPath);*/ //刪除測試-------------------------------------- FtpUtil ftputil=new FtpUtil(); boolean flag=ftputil.deleteFile("/2015/06/04/va.jpg"); System.out.println(flag); } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getBasePath() { return basePath; } public void setBasePath(String basePath) { this.basePath = basePath; } @Override public String toString() { return "FtpUtil [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password + ", basePath=" + basePath + "]"; } }
applicationContext.xml函數
<context:property-placeholder location="classpath:*.properties"/>
ftp.properties工具
ftp.host=192.168.100.77
ftp.port=21
ftp.username=ftpuser
ftp.password=ftp54321
ftp.basePath=/home/ftpuser/
xml方式
<bean id="ftpUtil" class="com.cky.util.FtpUtil"> <property name="host" value="${ftp.host}"></property> <property name="port" value="${ftp.port}"></property> <property name="username" value="${ftp.username}"></property> <property name="password" value="${ftp.password}"></property> <property name="basePath" value="${ftp.basePath}"></property> </bean>
註解方式,組件掃描
<context:component-scan base-package="com.cky.util"></context:component-scan>
@Autowired
private FtpUtil ftpUtil;
一、鏈接失敗
(1)檢查ftp服務器是否打開
(2)檢查防火牆是否將ftp服務器端口加入白名單(測試的話也能夠直接把防火牆關掉)
二、建立文件或者上傳文件失敗:
最多見的就是文件權限問題形成的,不光是讀寫權限,還有文件的用戶組。
例如:ftpClient登陸的用戶是ftpUser 通常是文件存放在/home/ftpUser中,通常咱們不會直接放在根目錄,而是建立對應文件夾,這時須要注意,建立文件夾時須要以ftpUser或者同一組用戶的身份建立,若是你是以root用戶建立的,那麼ftpUser將無權訪問!