예제 #1
0
 public static boolean mkdir(FTPClient ftp, String path) throws IOException {
   String[] pathNames = path.split("/");
   ftp.changeWorkingDirectory("/");
   for (int i = 0; i < pathNames.length; i++) {
     if (!ftp.changeWorkingDirectory(pathNames[i])) {
       if (FTPReply.isPositiveCompletion(ftp.mkd(pathNames[i]))) {
         ftp.changeWorkingDirectory(pathNames[i]);
       } else {
         FTPLogger.error("Make dir failed with path: " + pathNames[i]);
         return false;
       }
     }
   }
   return true;
 }
예제 #2
0
  private void uploadDir(File directory) throws IOException {

    try {
      String[] children = directory.list();

      if (children == null) {
        // Either dir does not exist or is not a directory
      } else {
        for (String filename : children) {
          File f = new File(directory, filename);
          if (f.isDirectory()) {
            ftpClient.mkd(f.getName());
            ftpClient.changeWorkingDirectory(f.getName());

            uploadDir(f);

            ftpClient.changeToParentDirectory();
          } else {
            if (!uploaded.contains(filename)) {
              LOGGER.debug("uploading file: {}", filename);
              messageListener.addMessage(new Message("uploading file: " + filename));

              ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
              ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

              ftpClient.enterLocalPassiveMode();
              InputStream is =
                  new BufferedInputStream(new FileInputStream(directory + "/" + filename));
              ftpClient.storeFile(filename, is);

              uploaded.add(filename);
            }
          }
        }
      }
    } catch (IOException e) {
      LOGGER.error("Upload failure");
      throw new IOException(e);
    }
  }