一个FTP客户端demo

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("连接失败");
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi 10.1 Berlin是一款强大的集成开发环境,提供了丰富的组件和工具来帮助开发者快速地创建各种类型的应用程序。其中,IDFTP(Internet Direct File Transfer Protocol)组件可以用来实现FTP(文件传输协议)的功能。 在Delphi 10.1 Berlin中,我们可以使用IDFTP组件来创建一个简单的FTP客户端程序的Demo。首先,我们需要在界面上放置一个TIDFTP组件,并设置好FTP服务器的地址、端口、用户名和密码等信息。接着,我们可以使用TButton组件来实现上传文件、下载文件、创建目录、删除文件等操作的按钮。 下面是一个简单的IDFTPDemo示例代码: ```pascal procedure TForm1.Button1Click(Sender: TObject); begin if not IDFTP1.Connected then begin IDFTP1.Host := 'ftp.example.com'; IDFTP1.Port := 21; IDFTP1.Username := 'username'; IDFTP1.Password := 'password'; IDFTP1.Connect; end; end; procedure TForm1.Button2Click(Sender: TObject); begin IDFTP1.Get('remote_file.txt', 'local_file.txt'); end; procedure TForm1.Button3Click(Sender: TObject); begin IDFTP1.Put('local_file.txt', 'remote_file.txt'); end; procedure TForm1.Button4Click(Sender: TObject); begin IDFTP1.MakeDir('new_directory'); end; procedure TForm1.Button5Click(Sender: TObject); begin IDFTP1.Delete('remote_file.txt'); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if IDFTP1.Connected then IDFTP1.Disconnect; end; ``` 上面的示例代码演示了如何在Delphi 10.1 Berlin中使用IDFTP组件来实现连接FTP服务器、上传下载文件、创建删除目录等操作的Demo。通过这个示例,开发者可以快速上手并定制自己的FTP客户端程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值