Java实现Ftp上传和下载文件(依赖commons-net-3.3.jar)--FTPUtil.java

3 篇文章 0 订阅

下载commons-net-3.3.jar

FTPUtil.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;

public class FTPUtil {

    private static Logger log = Logger.getLogger("FTPUtil.class");

    private String host;
    private int port;
    private String username;
    private String password;

    public FTPUtil(String host, String port, String username, String password) {
        this.host = host;
        this.port = Integer.parseInt(port);
        this.username = username;
        this.password = password;
    }

    public File downloadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
        FTPClient ftpClient = new FTPClient();
        FileOutputStream outputStream = null;
        File file = null;
        try {
            log.info(">>>>>>>>FTP-->downloadFile--登录开始>>>>>>>>>>>>>");

            ftpClient.connect(host, port);
            //设置ftp连接模式 被动模式
            ftpClient.enterLocalPassiveMode();

            boolean login = ftpClient.login(username, password);

            if (login) {
                log.info(">>>>>>>>FTP-->downloadFile--登录成功>>>>>>>>>>>>>");
            }
            else {
                log.info(">>>>>>>>FTP-->downloadFile--登录失败>>>>>>>>>>>>>");
                throw new RuntimeException("FTP登陆失败,请检查用户信息!");
            }

            boolean isDownload = false;

            file = new File(localPath + localFileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            //切换文件路径
            ftpClient.makeDirectory(remotePath);
            ftpClient.changeWorkingDirectory(remotePath);

            FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile ftpFile : ftpFiles) {
                if (ftpFile.getName().equals(remoteFileName)) {
                    outputStream = new FileOutputStream(file);
                    isDownload = ftpClient.retrieveFile(ftpFile.getName(), outputStream);
                }
            }

            if (isDownload) {
                log.info(">>>>>>>>FTP-->downloadFile--文件下载成功!本地路径:" + file);
            }
            else {
                throw new RuntimeException("FTP-->downloadFile--文件下载失败!请检查!");
            }

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                outputStream.close();
                ftpClient.disconnect();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public void uploadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
        FTPClient ftpClient = new FTPClient();
        FileInputStream inputStream = null;
        log.info(">>>>>>>>FTP-->uploadFile--登录开始>>>>>>>>>>>>>");

        ftpClient.enterLocalPassiveMode();//设置成被动FTP模式
        try {
            boolean login = ftpClient.login(username, password);
            if (login) {
                log.info(">>>>>>>>FTP-->uploadFile--登录成功>>>>>>>>>>>>>");
            }
            else {
                log.info(">>>>>>>>FTP-->uploadFile--登录失败>>>>>>>>>>>>>");
                throw new RuntimeException("FTP登陆失败,请检查用户信息!");
            }

            //切换文件路径
            ftpClient.makeDirectory(remotePath);
            ftpClient.changeWorkingDirectory(remotePath);
            inputStream = new FileInputStream(new File(localPath + localFileName));
            //可上传多文件
            boolean isUpload = ftpClient.storeFile(remoteFileName, inputStream);

            if (isUpload) {
                log.info(">>>>>>>>FTP-->uploadFile--文件上传成功!");
            } else {
                log.info(">>>>>>>>FTP-->uploadFile--文件上传失败!");
                throw new RuntimeException("文件上传失败!");
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                inputStream.close();
                ftpClient.disconnect();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}



  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值