一个FTP客户端demo

    xiaoxiao2021-12-14  20

    package com.device.net.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; /** * @author yangZ * */ public class FtpClient{ private FTPClient ftpClient; private FileInputStream fis; private FileOutputStream fos; public FTPClient getFtpClient() { return ftpClient; } public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } public FtpClient(int bufferSize,String encoding){ ftpClient=new FTPClient(); ftpClient.setBufferSize(bufferSize); ftpClient.setControlEncoding(encoding); } public FtpClient() { ftpClient=new FTPClient(); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("UTF-8"); } /** * 设置被动模式,默认主动模式 * (需要连接成功后设置,否则设置无效) * @return */ public boolean setPassiveMode(){ ftpClient.enterLocalPassiveMode(); if(ftpClient.getDataConnectionMode()==FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE){ return true; } return false; } public boolean connect(String ip, String username, String password){ try { ftpClient.connect(ip); //访问ip if(!ftpClient.login(username, password)){//登录用户名 //登录密码 return false; } } catch (IOException e) { e.printStackTrace(); return false; } return true; } public boolean connect(String ip,int port, String username, String password){ try { ftpClient.connect(ip, port);; //访问ip if(!ftpClient.login(username, password)){//登录用户名 //登录密码 return false; } } catch (IOException e) { e.printStackTrace(); return false; } return true; } public boolean disconnect(){ try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); return false; } return true; } public boolean changeWorkingDirectory(String directory){ try { if(!ftpClient.changeWorkingDirectory(directory)){ return false; } } catch (IOException e) { e.printStackTrace(); return false; } return true; } /** * 上传文件 * @param url * @param upFileName * @param fileType * @return */ public boolean upload(String srcUrl,String remoteFileName,int fileType){ File srcFile = new File(srcUrl); try { fis = new FileInputStream(srcFile); if(!ftpClient.setFileType(fileType)){ System.out.println("设置文件传输类型失败"); return false; } if(!ftpClient.storeFile(remoteFileName, fis)){ System.out.println("上传失败"); return false; } } catch (Exception e) { e.printStackTrace(); if(fis!=null){ IOUtils.closeQuietly(fis); } return false; } if(fis!=null){ IOUtils.closeQuietly(fis); } return true; } public boolean upload(String srcUrl,String remoteFileName){ return upload(srcUrl,remoteFileName,FTPClient.BINARY_FILE_TYPE); } /** * 下载文件 * @param url * @param downFileName * @param fileType * @return */ public boolean download(String remoteFileUrl,String saveFileUrl,int fileType){ try { fos = new FileOutputStream(saveFileUrl); if(!ftpClient.setFileType(fileType)){ System.out.println("设置文件传输类型失败"); return false; } if(!ftpClient.retrieveFile(remoteFileUrl, fos)){ System.out.println("下载文件失败"); return false; } } catch (Exception e) { System.out.println("文件下载失败"); if(fos!=null){ IOUtils.closeQuietly(fos); } return false; } if(fos!=null){ IOUtils.closeQuietly(fos); } return true; } public boolean download(String remoteFileUrl,String saveFileUrl){ return download(remoteFileUrl,saveFileUrl,FTPClient.BINARY_FILE_TYPE); } public static void main(String[] args) { FtpClient ftpClient = new FtpClient(); if(ftpClient.connect("121.43.183.8","***", "****")){ System.out.println(ftpClient.setPassiveMode()?"设置被动模式成功":"设置被动模式失败"); System.out.println(ftpClient.changeWorkingDirectory("/")?"切换目录成功":"切换目录失败"); // System.out.println(ftpClient.upload("D:\\install.ini", "测试ftp上传文件")?"上传文件成功":"上传文件失败"); System.out.println(ftpClient.download("测试ftp上传文件", "C:\\Users\\admin\\Desktop\\测试ftp文件")?"下载文件成功":"下载文件失败"); System.out.println(ftpClient.disconnect()?"关闭连接成功":"关闭连接失败"); }else{ System.out.println("连接失败"); } } }
    转载请注明原文地址: https://ju.6miu.com/read-962839.html

    最新回复(0)