Java进行Ftp连接,进行文件传输等操作

ftp连接需要的jar包

<!-- Ftp 所依赖的jar包  begin-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>1.4.1</version>
        </dependency>
        <!-- Ftp 所依赖的jar包  end-->
public FTPClient ftpClient;

    /**
     * 获取ftp连接
     *
     * @param userName
     * @param password
     * @param ip
     * @param encoding
     * @param path
     * @return
     * @throws Exception
     */
    public boolean connectFtp(String userName, String password, String ip, String encoding, String path)
        throws Exception {
        ftpClient = new FTPClient();
        ftpClient.setControlEncoding(encoding);
        log.info("ftp连接地址:{},端口号:{},用户名:{},密码:{}", ip, 21, userName, password);
        ftpClient.connect(ip, 21);
        ftpClient.login(userName, password);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new Exception("连接超时");
        }
        if (!StringUtils.isEmpty(password)) {
            if (!ftpClient.changeWorkingDirectory(path)) {
                cdDirs(path);
            }
        }
        return true;
    }
/**
     * ftp上传文件或文件夹
     *
     * @param f
     * @throws Exception
     */
    public void upload(File f) throws Exception {
        if (f.isDirectory()) {
            ftpClient.makeDirectory(f.getName());
            ftpClient.changeWorkingDirectory(f.getName());
            File[] files = f.listFiles();
            for (File fstr : files) {
                upload(fstr);
            }
            ftpClient.changeToParentDirectory();
        } else {
            File file2 = new File(f.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftpClient.storeFile(file2.getName(), input);
            input.close();
        }
    }
/**
     * 【功能描述:删除文件夹】
     * 【功能详细描述:功能详细描述】
     *
     * @param ftpPath 文件夹的地址
     * @return true 表似成功,false 失败
     * @throws IOException
     * @see 【类、类#方法、类#成员】
     */
    public void delete(String ftpPath) throws IOException {
        FTPFile[] files = ftpClient.listFiles(ftpPath);
        for (FTPFile f : files) {
            String path = ftpPath + File.separator + f.getName();
            if (f.isFile()) {
                if (!ftpClient.deleteFile(path)) {
                    ftpClient.deleteFile(ftpPath);
                }
            } else if (f.isDirectory()) {
                delete(path);
            }
        }
        // 每次删除文件夹以后就去查看该文件夹下面是否还有文件,没有就删除该空文件夹
        FTPFile[] files2 = ftpClient.listFiles(ftpPath);
        if (files2.length == 0) {
            ftpClient.removeDirectory(ftpPath);
        }
    }
/**
     * 获取文件夹大小 单位byte
     *
     * @param filePath
     * @return
     */
    public Long getFileSize(String filePath) {
        Long size = 0L;
        try {
            ftpClient.changeWorkingDirectory(filePath);
            FTPFile[] listFiles = ftpClient.listFiles();
            for (FTPFile ftpFile : listFiles) {
                size += ftpFile.getSize();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }
发布了35 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tealala/article/details/103409032