springboot集成ftp
pom依賴包
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>
ftp登陸初始化
private FTPClient connectFtpServer(){ FTPClient ftpClient = new FTPClient(); ftpClient.setConnectTimeout(1000*30);//設置鏈接超時時間 ftpClient.setControlEncoding("utf-8");//設置ftp字符集 ftpClient.enterLocalPassiveMode();//設置被動模式,文件傳輸端口設置 try { ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//設置文件傳輸模式爲二進制,能夠保證傳輸的內容不會被改變 ftpClient.connect(url); ftpClient.login(userName,password); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)){ LOGGER.error("connect ftp {} failed",url); ftpClient.disconnect(); return null; } LOGGER.info("replyCode==========={}",replyCode); } catch (IOException e) { LOGGER.error("connect fail ------->>>{}",e.getCause()); return null; } return ftpClient; }
ftp上傳文件
/** * * @param inputStream 待上傳文件的輸入流 * @param originName 文件保存時的名字 */ public void uploadFile(InputStream inputStream, String originName){ FTPClient ftpClient = connectFtpServer(); if (ftpClient == null){ return; } try { ftpClient.changeWorkingDirectory(remoteDir);//進入到文件保存的目錄 Boolean isSuccess = ftpClient.storeFile(originName,inputStream);//保存文件 if (!isSuccess){ throw new BusinessException(ResponseCode.UPLOAD_FILE_FAIL_CODE,originName+"---》上傳失敗!"); } LOGGER.info("{}---》上傳成功!",originName); ftpClient.logout(); } catch (IOException e) { LOGGER.error("{}---》上傳失敗!",originName); throw new BusinessException(ResponseCode.UPLOAD_FILE_FAIL_CODE,originName+"上傳失敗!"); }finally { if (ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (IOException e) { LOGGER.error("disconnect fail ------->>>{}",e.getCause()); } } } }
ftp讀取文件,並轉成base64
/** * 讀ftp上的文件,並將其轉換成base64 * @param remoteFileName ftp服務器上的文件名 * @return */ public String readFileToBase64(String remoteFileName){ FTPClient ftpClient = connectFtpServer(); if (ftpClient == null){ return null; } String base64 = ""; InputStream inputStream = null; try { ftpClient.changeWorkingDirectory(remoteDir); FTPFile[] ftpFiles = ftpClient.listFiles(remoteDir); Boolean flag = false; //遍歷當前目錄下的文件,判斷要讀取的文件是否在當前目錄下 for (FTPFile ftpFile:ftpFiles){ if (ftpFile.getName().equals(remoteFileName)){ flag = true; } } if (!flag){ LOGGER.error("directory:{}下沒有 {}",remoteDir,remoteFileName); return null; } //獲取待讀文件輸入流 inputStream = ftpClient.retrieveFileStream(remoteDir+remoteFileName); //inputStream.available() 獲取返回在不阻塞的狀況下能讀取的字節數,正常狀況是文件的大小 byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes);//將文件數據讀到字節數組中 BASE64Encoder base64Encoder = new BASE64Encoder(); base64 = base64Encoder.encode(bytes);//將字節數組轉成base64字符串 LOGGER.info("read file {} success",remoteFileName); ftpClient.logout(); } catch (IOException e) { LOGGER.error("read file fail ----->>>{}",e.getCause()); return null; }finally { if (ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (IOException e) { LOGGER.error("disconnect fail ------->>>{}",e.getCause()); } } if (inputStream != null){ try { inputStream.close(); } catch (IOException e) { LOGGER.error("inputStream close fail -------- {}",e.getCause()); } } } return base64; }
ftp下載文件
/** * 文件下載 * @param remoteFileName ftp上的文件名 * @param localFileName 本地文件名 */ public void download(String remoteFileName,String localFileName){ FTPClient ftpClient = connectFtpServer(); if (ftpClient == null){ return ; } OutputStream outputStream = null; try { ftpClient.changeWorkingDirectory(remoteDir); FTPFile[] ftpFiles = ftpClient.listFiles(remoteDir); Boolean flag = false; //遍歷當前目錄下的文件,判斷是否存在待下載的文件 for (FTPFile ftpFile:ftpFiles){ if (ftpFile.getName().equals(remoteFileName)){ flag = true; break; } } if (!flag){ LOGGER.error("directory:{}下沒有 {}",remoteDir,remoteFileName); return ; } outputStream = new FileOutputStream(localDir+localFileName);//建立文件輸出流 Boolean isSuccess = ftpClient.retrieveFile(remoteFileName,outputStream); //下載文件 if (!isSuccess){ LOGGER.error("download file 【{}】 fail",remoteFileName); } LOGGER.info("download file success"); ftpClient.logout(); } catch (IOException e) { LOGGER.error("download file 【{}】 fail ------->>>{}",remoteFileName,e.getCause()); }finally { if (ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (IOException e) { LOGGER.error("disconnect fail ------->>>{}",e.getCause()); } } if (outputStream != null){ try { outputStream.close(); } catch (IOException e) { LOGGER.error("outputStream close fail ------->>>{}",e.getCause()); } } } }
ftp客戶端與服務端之間數據傳輸,主動模式和被動模式
請移至這個哥們的博文中查看,https://www.cnblogs.com/mawanglin2008/articles/3607767.html,寫的很是好。java
主動模式:ftp客戶端產生一個隨機端口,並告知ftp服務端,最後服務端的20端口會與這個隨機端口創建鏈接,進行數據傳輸spring
被動模式:ftp服務端產生一個隨機端口,並告知ftp客戶端,最後客戶端與這個隨機端口創建鏈接,進行數據傳輸數組