Java讀取傳輸FTP文件實現(xiàn)示例
一.需求
FTP作為文件服務(wù)器,由提供服務(wù)方提供遠(yuǎn)程連接地址,連接端口,賬號,密碼等信息。
根據(jù)以上信息可以建立客戶端連接,隨后對于建立好的連接可進(jìn)行文件讀取,文件上傳等操作
二.依賴
<!-- FTP相關(guān)操作的依賴 --> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.9.0</version> </dependency> <!-- IO工具類的依賴 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> <!-- lombok依賴 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> <scope>provided</scope> </dependency>
三.關(guān)鍵源碼
3.0 配置信息
ftp: config: host: ${env.ftp.config.host:10.1.1.1} port: ${env.ftp.config.port:12345} username: ${env.ftp.config.username:ftpUsername} password: ${env.ftp.config.password:ftpPwd} remote-dir-path: ${env.ftp.config.remote-dir-path:/}
/** * @author: Vainycos * @description ftp配置信息 * @date: 2023/4/17 15:16 */ @Data @Component @ConfigurationProperties("ftp.config") public class FtpConfig { private String host; private int port; private String username; private String password; /** 初始讀取根目錄,當(dāng)前默認(rèn)/ */ private String remoteDirPath; }
3.1 獲取客戶端
/** * 獲取ftp客戶端 * @return */ public FTPClient getFtpClient(){ try { FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpConfig.getHost() ,ftpConfig.getPort()); // 10分鐘連接時間 ftpClient.setConnectTimeout(600000); ftpClient.setDefaultTimeout(600000); ftpClient.login(ftpConfig.getUsername() ,ftpConfig.getPassword()); // login后設(shè)置傳輸?shù)哪J? ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // login后設(shè)置被動模式 ftpClient.enterLocalPassiveMode(); // login后設(shè)置編碼 String LOCAL_CHARSET = "GBK"; // 開啟服務(wù)器對UTF-8的支持,如果服務(wù)器支持就用UTF-8編碼,否則就使用本地編碼(GBK). if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) { LOCAL_CHARSET = "UTF-8"; } ftpClient.setControlEncoding(LOCAL_CHARSET); if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { log.error("未連接到FTP,用戶名或密碼錯誤!"); ftpClient.disconnect(); } else { log.info("FTP連接成功!"); } // 切換從某個根目錄下開始掃描 ftpClient.changeWorkingDirectory(ftpConfig.getRemoteDirPath()); return ftpClient; }catch (IOException e) { log.error("ftp建立連接異常->{}", e); } return null; }
3.2 獲取ftp文件
/** * 獲取對應(yīng)目錄下的第一級目錄文件 * @param ftpClient client * @throws IOException */ public void getFtpFirstDirectoryFiles(FTPClient ftpClient) throws IOException { ? ? log.info("ftpclient當(dāng)前工作目錄->{}", ftpClient.printWorkingDirectory()); ? ? if (ftpClient != null) { ? ? ? ? FTPFile[] files = ftpClient.listFiles(); ? ? ? ? for (FTPFile file : files) { ? ? ? ? ? ? String fileName = file.getName(); ? ? ? ? ? ? if (file.isDirectory()){ ? ? ? ? ? ? ? ? // 每次從根目錄下查找第一級目錄 ? ? ? ? ? ? ? ? String firstDirectory = ftpConfig.getRemoteDirPath() + "/" + fileName; ? ? ? ? ? ? ? ? ftpClient.changeWorkingDirectory(firstDirectory); ? ? ? ? ? ? ? ? log.info("當(dāng)前目錄->{}, 開始掃描錄音文件", firstDirectory); ? ? ? ? ? ? ? ? // 切換目錄后直接遍歷第一級的文件,不遞歸第二級目錄 ? ? ? ? ? ? ? ? dealFile(ftpClient); ? ? ? ? ? ? ? ? log.info("{}->目錄掃描結(jié)束", firstDirectory); ? ? ? ? ? ? } ? ? ? ? } ? ? } } /** ?* 處理目錄下的文件 ?* @param ftpClient ?* @throws IOException ?*/ public void dealFile(FTPClient ftpClient) throws IOException { ?? ?FTPFile[] files = ftpClient.listFiles(); ? ? ? ? for (FTPFile file : files) { ?? ? ? ? ? ?String fileName = file.getName(); ? ? ? ? ? ? if (file.isDirectory()) { ? ? ? ? ? ? ? ? log.info("{}->為目錄,跳過", fileName); ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? } ? ? ? ? ? ? String rootWorkingDirectory = ftpClient.printWorkingDirectory(); ? ? ? ? ? ? log.info("獲取到文件->{}, 開始獲取ftp文件流, ftpclient工作目錄->{}", fileName, ftpClient.printWorkingDirectory()); ? ? ? ? ? ? // 開始獲取ftp文件流 ? ? ? ? ? ? InputStream inputStream = ftpClient.retrieveFileStream(new String(fileName.getBytes("UTF-8"), FTP.DEFAULT_CONTROL_ENCODING)); ? ? ? ? ? ? if (inputStream == null){ ? ? ? ? ? ? ? ? log.error("文件不存在->{}", fileName); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? byte[] data = IOUtils.toByteArray(inputStream); ? ? ? ? ? ? inputStream.close(); ? ? ? ? ? ? // 關(guān)鍵代碼,如果不執(zhí)行該代碼,后續(xù)的ftpClient操作將會不生效 ? ? ? ? ? ? ftpClient.completePendingCommand(); ? ? ? ? ? ? // 省略...獲取到了inputStream 文件流進(jìn)行后續(xù)處理 ? ? ? ??? ?} }
3.3 關(guān)閉ftp服務(wù)連接
/** * 關(guān)閉FTP服務(wù)連接 * @param ftpClient */ public void disConnection(FTPClient ftpClient) { try{ if(ftpClient.isConnected()){ ftpClient.disconnect(); } }catch(IOException e) { log.error("ftpClient.disconnect失敗->{}", e); } }
四.總結(jié)
上述源碼僅供參考,具體需根據(jù)實際業(yè)務(wù)需求進(jìn)行調(diào)整。主要注意點為讀取中文文件名的文件以及獲取ftpClient文件流之后的處理,已標(biāo)注在注釋中,希望能夠幫助讀者少踩坑。
參考資料
基于ftp協(xié)議的文件變化主動監(jiān)聽
FTPSClient解決無法獲取文件問題(listFiles為空)
ftpClient.retrieveFileStream導(dǎo)致FTPClient的后面操作失敗
到此這篇關(guān)于Java讀取傳輸FTP文件實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java讀取傳輸FTP文件 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何基于回調(diào)實現(xiàn)Java的異步調(diào)用
這篇文章主要介紹了如何基于回調(diào)實現(xiàn)Java的異步調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06IDEA SpringBoot:Cannot resolve configuration&
這篇文章主要介紹了IDEA SpringBoot:Cannot resolve configuration property配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07