Java实现上传文件到FTP服务器

1、文章的基础是Maven项目,添加依赖

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

2、实现代码

/**
 * ftp相关dto
 */
public class FtpDto {

    private String ipAddr;//ip地址

    private Integer port;//端口号

    private String userName;//用户名

    private String pwd;//密码

    private String path;//路径

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}

工具类实现:

import com.wd.kdm.common.dto.FtpDto;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Ftp工具类
 */
public class FtpUtil {

    private static FTPClient ftp;

    /**
     * 获取ftp连接
     * @param ftpDto
     * @return
     * @throws Exception
     */
    public static boolean connectFtp(FtpDto ftpDto) throws Exception {
        ftp = new FTPClient();
        boolean flag = false;
        int reply;
        if (ftpDto.getPort() == null) {
            ftp.connect(ftpDto.getIpAddr(), 21);
        } else {
            ftp.connect(ftpDto.getIpAddr(), ftpDto.getPort());
        }
        ftp.login(ftpDto.getUserName(), ftpDto.getPwd());
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        //得到相应结果:200为正常
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return flag;
        }
        ftp.changeWorkingDirectory(ftpDto.getPath());
        flag = true;
        return flag;
    }

    /**
     * 关闭ftp连接
     */
    public static void closeFtp() {
        if (ftp != null && ftp.isConnected()) {
            try {
                ftp.logout();
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ftp上传文件
     * @param file
     * @throws Exception
     */
    public static void upload(File file) throws Exception {
        if (file.isDirectory()) {
            //ftp创建文件夹
            ftp.makeDirectory(file.getName());
            //ftp切换工作路径
            ftp.changeWorkingDirectory(file.getName());
            //获取上传文件集合
            String[] files = file.list();
            for (String fstr : files) {
                File file1 = new File(file.getPath() + "/" + fstr);
                if (file1.isDirectory()) {
                    upload(file1);
                    //转换工作路径
                    ftp.changeToParentDirectory();
                } else {
                    File file2 = new File(file.getPath() + "/" + fstr);
                    FileInputStream input = new FileInputStream(file2);
                    //ftp上传文件
                    ftp.storeFile(file2.getName(), input);
                    input.close();
                }
            }
        } else {
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftp.storeFile(file2.getName(), input);
            input.close();
        }
    }
    /**
     * 测试
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{
        FtpDto f = new FtpDto();
        f.setIpAddr("your ip");
        f.setUserName("username");
        f.setPwd("password");
        FtpUtil.connectFtp(f);
        File file = new File("F:/commons-net-3.6.jar");
        FtpUtil.upload(file);//把文件上传在ftp上
        System.out.println("上传文件完成。。。。");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值