java实现ftp文件上传,下载

1. 首先先配置ftp服务器
可参考:https://www.cnblogs.com/popfisher/p/7992036.html

2. 需要依赖:commons-net,版本3.6

gradle添加依赖
compile group: 'commons-net', name: 'commons-net', version: "3.6"

3. 代码实现

/**
 * 使用 FTP 实现文件上传下载
 */

public static boolean uploadFile(String hostname, int port, String username, String password, String pathname,
            String fileName, InputStream inputStream) {
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("UTF-8");
        try {
            // 连接FTP服务器
            ftpClient.connect(hostname, port);
            // 登录FTP服务器
            ftpClient.login(username, password);
            // 是否成功登录FTP服务器
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
               ftpClient.disconnect();
               return flag;
            }

            // 设置传输二进制文件
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }


public static boolean downloadFile(String hostname, int port, String username, String password, String pathname,
            String filename, String localpath) {
        boolean flag = false;
        FTPClient ftpClient = new FTPClient();
        try {
            // 连接FTP服务器
            ftpClient.connect(hostname, port);
            // 登录FTP服务器
            ftpClient.login(username, password);
            // 验证FTP服务器是否登录成功
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                return flag;
            }
            // 切换FTP目录
            ftpClient.changeWorkingDirectory(pathname);
           
            File localFile = new File(localpath + "/" + file.getName());
            OutputStream os = new FileOutputStream(localFile);
            ftpClient.retrieveFile(file.getName(), os);
            os.close();
       
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {

                }
            }
        }
        return flag;
    }


4. 
设置连接超时和数据传输超时,对于性能有要求的项目,设置这两个属性很重要

例如,设置为60秒:
ftpClient.setDataTimeout(60000);       //设置传输超时时间为60秒 
ftpClient.setConnectTimeout(60000);       //连接超时为60秒

5. 
需要注意的是,ftp.isConnected最好要写在finally里面,不要直接写在ftp.logout()后面

因为这样子写会有很大的隐患,查看org.apache.commons.net的API,可以看到,FTPClient的logout()也
会抛出IOException的。
如上述代码的直接后果就是,Ftpclient对象退出异常,连接没有释放。积少成多,也会慢慢阻塞FTP服务器的
连接,最终引发连接异常。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DQchat

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

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

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

打赏作者

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

抵扣说明:

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

余额充值