基于Java手寫一個(gè)好用的FTP操作工具類
前言
網(wǎng)上百度了很多FTP的java 工具類,發(fā)現(xiàn)文章代碼都比較久遠(yuǎn),且代碼臃腫,即使搜到了代碼寫的還可以的,封裝的常用操作方法不全面,于是自己花了半天實(shí)現(xiàn)一個(gè)好用的工具類。最初想用java自帶的FTPClient 的jar 去封裝,后來和apache的jar工具包對比后,發(fā)現(xiàn)易用性遠(yuǎn)不如apache,于是決定采用apache的ftp的jar 封裝ftp操作類。
windows服務(wù)器搭建FTP服務(wù)
打開控制版面,圖示win 10為例。
點(diǎn)擊程序
選擇 啟用或者關(guān)閉Windows 功能
勾選啟用 Internet Information Services 下FTP相關(guān)服務(wù)和 IIS 管理控制平臺(tái)還有萬維網(wǎng)服務(wù) 后,點(diǎn)擊確定。
打開 IIS管理器
選中網(wǎng)站,鼠標(biāo)右鍵 ,添加 FTP 站點(diǎn)
添加 網(wǎng)站名稱,選擇本地物理路徑 ,設(shè)置完畢,點(diǎn)擊。
填寫自己的內(nèi)網(wǎng)ip,選擇 無 SSL,點(diǎn)擊下一步。
勾選匿名 (訪問時(shí)候不需要賬戶密碼驗(yàn)證),允許所有用戶 ,選擇 讀取 和寫入權(quán)限(根據(jù)自己需求選擇),點(diǎn)擊完成。
同一內(nèi)網(wǎng)的任何電腦的文件夾 內(nèi)輸入 自己設(shè)置的ip和端口 ftp://ip:port ,即可訪問。
工具類方法
- 賬戶密碼登錄方法
- 無賬號密碼登錄方法
- 字符轉(zhuǎn)碼方法
- 判斷文件目錄是否存在方法
- 獲取文件列表方法
- 上傳文件方法
- 下載文件方法
- 上傳文件夾方法
- 下載文件夾方法
- 刪除文件方法
- 刪除文件夾方法
- 創(chuàng)建文件夾方法
- 文件重命名方法
代碼展示
pom文件引入依賴關(guān)系 commons-net jar
<!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>
工具類完整代碼
import org.apache.commons.net.ftp.*; import java.io.*; import java.util.ArrayList; import java.util.List; /** * Java FTP工具類 */ public class FTPUtil { private static FTPClient ftp; /** * 方法描述: 轉(zhuǎn)碼 */ private static String transcode(String text){ try { return new String(text.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING); } catch (UnsupportedEncodingException e) { return null; } } /** * 方法描述: 連接 ftp服務(wù)器 匿名登錄無密碼 */ public static void connectServer(String ip, int port) throws IOException { connectServer(ip,port,"anonymous",null); } /** * 方法描述: 連接 ftp服務(wù)器 */ public static void connectServer(String ip, int port, String user, String password) throws IOException { // 連接ftp服務(wù)器 ftp = new FTPClient(); ftp.connect(ip, port); // 登錄ftp服務(wù)器 ftp.login(user, password); //設(shè)置編碼 ftp.setControlEncoding("GBK"); //設(shè)置文件類型 ftp.setFileType(FTP.BINARY_FILE_TYPE); } /** * 關(guān)閉連接 */ public static void closeServer() throws IOException { if (ftp.isConnected()) { ftp.logout(); ftp.disconnect(); } } /** * 判斷目錄是否存在 */ public static boolean existDirectory(String pathname) throws IOException { boolean flag = false; FTPFile[] ftpFileArr = ftp.listFiles(pathname); for (FTPFile ftpFile : ftpFileArr) { if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(pathname)) { flag = true; break; } } return flag; } /* * 獲取文件列表 */ public static List<String> listFiles(String path) throws IOException { FTPFile[] ftpFiles = ftp.listFiles(path); List<String> retList = new ArrayList<String>(); for (FTPFile ftpFile : ftpFiles) { retList.add(ftpFile.getName()); } return retList; } /** * 上傳文件 */ public static boolean uploadFile(String remote,String local) throws IOException { InputStream is=new FileInputStream(local); return ftp.storeFile(transcode(remote),is); } /** * 下載文件 */ public static boolean downloadFile(String remote,String local) throws IOException { OutputStream out=new FileOutputStream(local); return ftp.retrieveFile(transcode(remote),out); } /** * 刪除文件 */ public static boolean deleteFile(String remote) throws IOException { return ftp.deleteFile(transcode(remote)); } /** * 刪除文件夾 */ public static void deleteFolder(String remote) throws IOException { FTPFile[] ftpFiles=ftp.listFiles(transcode(remote)); for (FTPFile ftpFile : ftpFiles) { if(ftpFile.isDirectory()){ deleteFolder(remote+"/"+ftpFile.getName()); ftp.removeDirectory(transcode(remote+"/"+ftpFile.getName())); }else{ deleteFile(ftpFile.getName()); } } ftp.removeDirectory(transcode(remote)); } /** * 上傳文件夾到ftp服務(wù)器 */ public static void uploadFolder(String remote,String local) throws IOException { File localFile=new File(local); if(localFile.isDirectory()){ String remoteDir=remote+"/"+localFile.getName(); makeDirectory(remoteDir); File[] partFiles=localFile.listFiles(); for (File file : partFiles) { if(file.isDirectory()){ uploadFolder(remoteDir+"/"+file.getName(),local+"/"+file.getName()); }else { uploadFile(remoteDir+"/"+file.getName(),local+"/"+file.getName()); } } } } /** * 下載文件夾到本地 */ public static void downloadFolder(String remote,String local) throws IOException { File localFile=new File(local); if(!localFile.exists()){ localFile.mkdirs(); } FTPFile[] ftpFiles=ftp.listFiles(transcode(remote)); for (FTPFile ftpFile : ftpFiles) { if(ftpFile.isDirectory()){ downloadFolder(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName()); }else { downloadFile(remote+"/"+ftpFile.getName(),local+"/"+ftpFile.getName()); } } } /** * 創(chuàng)建文件夾 */ public static void makeDirectory(String remote) throws IOException { if(remote.startsWith("/")){ remote=remote.substring(1); } String[] dirNames = remote.split("/"); String tempPath=""; for (String dirName : dirNames) { tempPath=tempPath+"/"+dirName; ftp.makeDirectory(transcode(tempPath)); } } /** * 重命名 */ public static boolean rename(String from, String to) throws IOException { return ftp.rename(transcode(from),transcode(to)); } }
使用示例
public static void main(String[] args) throws IOException { //匿名免密碼登錄 FTPUtil.connectServer("172.16.10.201",19001,"anonymous",null); //下載ftp根目錄所有文件到本地文件夾 FTPUtil.downloadFolder("/","D://ftp"); //刪除文件夾以及文件 FTPUtil.deleteFolder("tarzan"); //創(chuàng)建文件夾 FTPUtil.makeDirectory("tarzan/cms"); //文件夾或文件重命名 FTPUtil.rename("泰山","泰山123"); //上傳文件夾 FTPUtil.uploadFolder("software","D:\\Git"); }
到此這篇關(guān)于基于Java手寫一個(gè)好用的FTP操作工具類的文章就介紹到這了,更多相關(guān)Java FTP操作工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC框架post提交數(shù)據(jù)庫出現(xiàn)亂碼解決方案
這篇文章主要介紹了SpringMVC框架post提交數(shù)據(jù)庫出現(xiàn)亂碼解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09springboot后端如何實(shí)現(xiàn)攜帶token登陸
這篇文章主要介紹了springboot后端如何實(shí)現(xiàn)攜帶token登陸,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法
今天小編就為大家分享一篇關(guān)于Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01