Java 实现FTP 上传文件 下载文件

package ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.*;
import java.nio.charset.Charset;

public class FtpServer {

     //ftp对象
    private static FTPClient ftp;
    //需要连接到的ftp端的ip
    private String ip;
    //连接端口,默认21
    private int port=21;
    //要连接到的ftp端的名字
    private String name;
    //要连接到的ftp端的对应得密码
    private String pwd;

    public FtpServer(String ip, String name, String pwd) {
        this(ip,21,name,pwd);
    }

    public FtpServer(String ip, int port, String name, String pwd) {
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.pwd = pwd;
        this.init();
    }

    private void init(){
        ftp = new FTPClient();
        //验证登录
        try {
            ftp.connect(ip, port);
            ftp.login(name, pwd);
            ftp.setCharset(Charset.forName("UTF-8"));
            ftp.setControlEncoding("UTF-8");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new  RuntimeException("连接失败");
        }
    }

    /**
     * 上传文件
     * @param SourcePath 本地文件
     *
     * @param TargetPath FTP服务器文件
     */
    public void UploadFile(String SourcePath,String TargetPath){
        try {

            OutputStream os = ftp.storeFileStream(TargetPath);
            FileInputStream fis = new FileInputStream(SourcePath);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                os.write(b,0,len);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw  new RuntimeException("UploadFile 上传文件失败"+e.getMessage());
        }
    }

    /**
     * 重命名
     * @param OldName 旧名称
     * @param NewName 新名称
     */
    public void Rename(String OldName,String NewName){

    }

    /**
     * 下载文件
     * @param SourcePath 本地文件
     * @param TargetPath FTP文件
     */
    public void DownloadFile(String SourcePath,String TargetPath){
        try {
            InputStream is = ftp.retrieveFileStream(TargetPath);
            FileOutputStream fos = new FileOutputStream(new File(SourcePath));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = is.read(b)) != -1) {
                fos.write(b,0,len);
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw  new RuntimeException("DownloadFile 下载文件失败"+e.getMessage());
        }
    }

    /**
     *  删除文件
     * @param DelPath 删除文件路径
     */
    public void DeleteFile(String DelPath){
        try {
            ftp.deleteFile(DelPath);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(String.format("删除文件异常%s",e.getMessage()));
        }
    }

    /**
     * 获取根目录全部文件目录文件名称
     */
    public void FileList(){
        FileList("/");
    }

    /**
     * 获取选中文件节点下所以文件目录名称
     * @param DirName
     */
    public void FileList(String DirName){
        FTPFile[] files = new FTPFile[0];
        try {
            files = ftp.listFiles(DirName);
            //打印出ftp里面,“Windows”文件夹里面的文件名字
            for (FTPFile file : files) {
                System.out.println(file.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw  new RuntimeException(String.format("FileList错误%s",e.getMessage()));
        }
    }

    /**
     * 判断文件是否存在
     * @param FilePath
     * @return
     */
    public boolean FileExists(String FilePath){
        try {
            FTPFile[] files =ftp.listFiles(FilePath);
            if(files!=null&&files.length>0){
                return true;
            }else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw  new RuntimeException(String.format("FileExists 文件是否存在异常 %s",e.getMessage()));
        }
    }

    public static void main(String args[]) {
        String ip="172.16.11.72";//临时域名
        String username="969721569@qq.com";//用户名
        String password="******";//密码
        int port =21;

        FtpServer m = new FtpServer(ip,port,username,password);
        //获取列表
        //m.FileList("/eosbparse");
        //m.FileList();
        //上传文件
        //m.UploadFile("C:\\Users\\xymgk\\Downloads\\ARJ21-A-91-11-00-33A-25BA-A.zip","\\eosbparse\\ARJ21-A-91-11-00-33A-25BA-A.zip");
        //下载文件
        //m.DownloadFile("D:\\ARJ21-A-91-11-00-33A-25BA-A.zip","\\eosbparse\\ARJ21-A-91-11-00-33A-25BA-A.zip");
        //删除文件
        //m.DeleteFile("\\eosbparse\\ARJ21-A-91-11-00-33A-25BA-A.zip");
        //判断文件是否存在
        System.out.println(m.FileExists("/eosbparse/cs1234.xml"));
    }
}

Maven引用

 <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

FTP 搭建参考

https://hsk.oray.com/news/7685.html 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值