java利用ftpClient来创建多层目录文件
java利用ftpClient来创建多层目录文件
啊哈关关 发表于9个月前
java利用ftpClient来创建多层目录文件
  • 发表于 9个月前
  • 阅读 1183
  • 收藏 0
  • 点赞 0
  • 评论 0
//改变目录路径
 public boolean changeWorkingDirectory(String directory) {
        boolean flag = true;
        try {
            flag = ftpClient.changeWorkingDirectory(directory);
            if (flag) {
                logger.debug("进入文件夹" + directory + " 成功!");

            } else {
                logger.debug("进入文件夹" + directory + " 失败!");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return flag;
    }


//创建目录
    public boolean makeDirectory(String dir) {
        boolean flag = true;
        try {
            flag = ftpClient.makeDirectory(dir);
            if (flag) {
                logger.debug("创建文件夹" + dir + " 成功!");

            } else {
                logger.debug("创建文件夹" + dir + " 失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


//创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
    public boolean CreateDirecroty(String remote) throws IOException {
        boolean success = true;
        String directory = remote + "/";
//        String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
        // 如果远程目录不存在,则递归创建远程服务器目录
        if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
            int start = 0;
            int end = 0;
            if (directory.startsWith("/")) {
                start = 1;
            } else {
                start = 0;
            }
            end = directory.indexOf("/", start);
            String path = "";
            String paths = "";
            while (true) {

                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
                path = path + "/" + subDirectory;
                if (!existFile(path)) {
                    if (makeDirectory(subDirectory)) {
                        changeWorkingDirectory(subDirectory);
                    } else {
                        logger.debug("创建目录[" + subDirectory + "]失败");
                         changeWorkingDirectory(subDirectory);
                    }
                } else {
                    changeWorkingDirectory(subDirectory);
                }

                paths = paths + "/" + subDirectory;
                start = end + 1;
                end = directory.indexOf("/", start);
                // 检查所有目录是否创建完毕
                if (end <= start) {
                    break;
                }
            }
        }
        return success;
    }





//判断ftp服务器文件是否存在    
public boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        if (ftpFileArr.length > 0) {
            flag = true;
        }
        return flag;
    }


//上传单个文件
    public boolean uploadFile(File localFile, String remoteFile)
            throws IOException {
        boolean flag = false;
        InputStream in = new FileInputStream(localFile);
        String remote = new String(remoteFile.getBytes("GBK"), "iso-8859-1");
        try {
            if (ftpClient.storeFile(remote, in)) {
                flag = true;
                logger.debug(localFile.getAbsolutePath() + "上传文件成功!");
            } else {
                logger.debug(localFile.getAbsolutePath() + "上传文件失败!");
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }


//利用递归上传多个多目录文件
    public boolean uploadFiles(String localPathFileName, String remotePathFileName) throws IOException {
        //
        File local = new File(localPathFileName);

        if (local.isDirectory()) {
            try {
                this.ftpClient.changeWorkingDirectory("/");
                CreateDirecroty(remotePathFileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            File local1 = new File(localPathFileName);
            uploadFile(local1, remotePathFileName);
        }
        File sourceFile[] = local.listFiles();
        for (int i = 0; i < sourceFile.length; i++) {
            if (sourceFile[i].exists()) {
                String path = sourceFile[i].getAbsolutePath().substring(localPathFileName.length());
                if (sourceFile[i].isDirectory()) {
                    this.uploadFiles(sourceFile[i].getAbsolutePath(), remotePathFileName + path);
                } else {
                    if (!path.equals("/.DS_Store")) {
                        File file2 = new File(sourceFile[i].getAbsolutePath());
                        uploadFile(file2, remotePathFileName + path);
                    }
                }
            }
        }
        return true;
    }
共有 人打赏支持
粉丝 0
博文 87
码字总数 37656
×
啊哈关关
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: