為什麼需要執行緒池?什麼是池化技術?
阿新 • • 發佈:2022-05-15
前言
ftp伺服器進行互動關於ftp上傳下載的工具類大致有兩種。
第一種是單例模式的類。
第二種是另外定義一個Service,直接通過Service來實現ftp的上傳下載刪除。
這兩種感覺都有利弊。
第一種實現了程式碼複用,但是配置資訊全需要寫在類中,維護比較複雜。
第二種如果是spring框架,可以通過propertis檔案,動態的注入配置資訊,但是又不能程式碼複用。
所以我打算自己實現一個工具類,來把上面的兩種優點進行整合。順便把一些上傳過程中一些常見的問題也給解決了。
因為我使用的是spring框架,如果把工具類宣告為bean給spring管理,他預設就是單例的
依賴jar包
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>
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 + "]"; } }
FTP常見問題:
1、連線失敗
(1)檢查ftp伺服器是否開啟
(2)檢查防火牆是否將ftp伺服器埠加入白名單(測試的話也可以直接把防火牆關掉)
2、建立檔案或者上傳檔案失敗:
最常見的就是檔案許可權問題造成的,不光是讀寫許可權,還有檔案的使用者組。
例如:ftpClient登入的使用者是ftpUser 一般是檔案存放在/home/ftpUser中,一般我們不會直接放在根目錄,而是建立對應資料夾,這時需要注意,建立資料夾時需要以ftpUser或者同一組使用者的身份建立,如果你是以root使用者建立的,那麼ftpUser將無權訪問!