从ftp服务器实现简易上下载文件

前几天学习了一下用java实现ftp上传和下载文件的内容,学之前查了一下ftp,遇到了一些问题,首先说明,ftp和tomcat无法进行比较,因为ftp为专门上载下载文件、存储文件的服务器,而tomcat则是进行项目部署的服务器,也就是将项目打包成war包,放入webapps中进行访问的服务器,因为两者的作用并不相同,所以无法进行比较
在idea中新建一个Maven工程,添加依赖,依赖为:

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

1、和连接数据库步骤差不多,先找到IP地址,访问服务器端口,进行连接,然后进行登录,登录是必须步骤,否则会因为没有的登录而报错,然后验证登录返回码,判断是否登陆成功,代码如下:

    public ftpUtil(String ip,int port,String name,String pwd){//将加载写入构造器内
        ftp = new FTPClient();
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.pwd = pwd;

        try{
            System.out.println("connecting ftp server..." );
            ftp.connect(ip,port);
            ftp.login(name,pwd);//不登录会报错,若是本地ftp服务器,则直接用计算机管理员登录即可
            int replyCode = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)){
                System.out.println("connect ftp server failed..." );
                return;
            }
            System.out.println("connect ftp server success..." );
            ftp.setCharset(Charset.forName("UTF-8"));
            ftp.setControlEncoding("UTF-8");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

2、上传文件到服务器,由于可能有人直接上传包含文件的目录,所以,对上传的文件进行判断是否为目录,若是目录,则在服务器内创建目录,然后再进行判断,直到将上传的目录内的内容全部输入到ftp服务中

 public void putLoad(File f) throws IOException {
        if (f.isDirectory()){//判断文件是否是一个目录
            ftp.makeDirectory(f.getName());//如果是,则在ftp服务器中新建一个目录
            ftp.changeWorkingDirectory(f.getName());//切换到传过来的path,也就是ftp.makeDirectory刚创建的目录
            String [] files = f.list();//获取本目录下的本级全部文件名和目录名
            for(String fstr:files){
                File file1 = new File(f.getPath()+"/"+fstr);
                if (file1.isDirectory()){//判断本级的某一个文件是否是目录
                    putLoad(file1);
                    ftp.changeToParentDirectory();//将要设置的目录更改为当前目录的父级
                }else {
                    File file2 = new File(f.getPath()+"/"+fstr);//访问文件
                    FileInputStream input = new FileInputStream(file2);
                    ftp.storeFile(file2.getName(),input);//将文件上传到FTP服务器上
                    flag = false;
                    System.out.println("Upload File Completion...");
                    input.close();
                }
            }
        }else{
            File file2 = new File(f.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftp.storeFile(file2.getName(),input);
            flag = false;
            System.out.println("Upload File Completion...");
            input.close();
        }
    }

3、下载文件,下载文件相对于上载文件要简单一些,因为用户会直接在ftp中选择文件,然后直接对文件进行下载,不会牵涉到目录的问题,下载文件后在本地存储的位置也是用户自己可以进行编辑的,所以,只需要将ftp服务器上的文件的内容进行读取,然后将读取到的内容写入到本地创建的文件中

    public void downLoad(String ftppathname, String filename, String localpath)throws Exception{
        System.out.println("download file will begin...");
        ftp.changeWorkingDirectory(ftppathname);//切换到ftp服务器的目录
        FTPFile []ftpFiles = ftp.listFiles();
        for (FTPFile file: ftpFiles) {
            if (filename.equalsIgnoreCase(file.getName())){
                File localfile = new File(localpath+"/"+file.getName());
                FileOutputStream output = new FileOutputStream(localfile);//创建localfile文件
                ftp.retrieveFile(filename,output);//从ftp服务器中检索文件,写入FileOutputStream中(只能写入FileOutputStream)
                System.out.println("Download File Completion...");
                output.close();
            }
        }
        System.out.println();
    }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃小巴掌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值