實現FTP相關功能
1、下載相應的jar包
commons-net-3.6.jar
2、代碼實現
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.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * FTP 操作類 * @author d * @version 1.0 * 2019.12.12 */ public class FtpUtil { public static void main(String[] args) { FtpUtil ftpUtil = new FtpUtil(); ftpUtil.downLoadFile("/", "", "D:/test"); // ftpUtil.uploadFile("/1", "1.zip", "D:/test/1.zip"); // ftpUtil.deleteFile("/1", "1.zip"); } /** * Ftp 服務器地址 **/ public String hostname = "127.0.0.1"; /** * Ftp 端口號 **/ public int port = 21; /** * Ftp 登錄賬號 **/ public String username = "root"; /** * Ftp 登錄密碼 **/ public String password = "root"; /** * 設置緩沖區大小4M **/ private static final int BUFFER_SIZE = 1024 * 1024 * 4; /** * Ftp 操作對象 **/ public FTPClient ftpClient = null; /** * 連接FTP服務器 * * @param address 地址 * @param port 端口 * @param username 用戶名 * @param password 密碼 */ private void login() { ftpClient = new FTPClient(); ftpClient.setControlEncoding("utf-8"); try { ftpClient.connect(hostname, port); ftpClient.login(username, password); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//文件類型為二進制文件 ftpClient.setBufferSize(BUFFER_SIZE);//限制緩沖區大小 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { closeConnect(); System.out.println("FTP服務器連接失敗"); } } catch (Exception e) { System.out.println("FTP登錄失敗" + e.getMessage()); } } /** * 關閉FTP連接 */ private void closeConnect() { if (ftpClient != null && ftpClient.isConnected()) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { System.out.println("關閉FTP連接失敗" + e.getMessage()); } } } /** * 下載文件 * @param ftpPath FTP文件目錄 * @param fileName 需下載的文件名 * @param savePath 下載后的文件路徑 * @return 返回是否下載成功 true */ public Boolean downLoadFile(String ftpPath, String fileName, String savePath){ login(); OutputStream os = null; if (ftpClient != null) { try { //判斷是否存在該目錄 if (!ftpClient.changeWorkingDirectory(ftpPath)) { System.out.println("/" + ftpPath + "該目錄不存在"); return false; } ftpClient.enterLocalPassiveMode();//設置被動模式,開通一個端口來傳輸數據 FTPFile[] ftpFiles = ftpClient.listFiles(); // 判斷該目錄下是否有文件 if (ftpFiles == null || ftpFiles.length == 0) { System.out.println("/" + ftpPath + "該目錄下無文件"); return false; } for(FTPFile file : ftpFiles){ if(fileName.equals("") || fileName.equalsIgnoreCase(file.getName())){//文件名稱為"",下載指定文件 if(!file.isDirectory()){//是否文件夾 File saveFile = new File(savePath + "/" + file.getName()); os = new FileOutputStream(saveFile); ftpClient.retrieveFile(file.getName(), os); os.close(); } } } return true; } catch (IOException e) { System.out.println("下載文件失敗" + e.getMessage()); } finally { if(null != os){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } closeConnect(); } } return false; } /** * 上傳文件 * @param savePath FTP保存目錄 * @param fileName 上傳到FTP的文件名 * @param filePath 待上傳文件的名稱(絕對地址) * @return */ public boolean uploadFile(String savePath, String fileName,String filePath){ login(); boolean flag = false; InputStream inputStream = null; if (ftpClient != null) { try{ inputStream = new FileInputStream(new File(filePath)); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.makeDirectory(savePath); ftpClient.changeWorkingDirectory(savePath); ftpClient.storeFile(fileName, inputStream); inputStream.close(); flag = true; }catch (Exception e) { e.printStackTrace(); }finally{ if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } closeConnect(); } } return flag; } /** * 刪除文件 * * @param pathname FTP服務器保存目錄 * @param filename 要刪除的文件名稱 * @return */ public boolean deleteFile(String filePath, String filename){ login(); boolean flag = false; if (ftpClient != null) { try { ftpClient.changeWorkingDirectory(filePath); ftpClient.dele(filename); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { closeConnect(); } } return flag; } }