Esempio n. 1
0
  /**
   * Renames the file denoted by this abstract pathname.
   *
   * <p>Whether or not this method can move a file from one filesystem to another is
   * platform-dependent. The return value should always be checked to make sure that the rename
   * operation was successful.
   *
   * @param dest The new abstract pathname for the named file
   * @throws IllegalArgumentException If parameter <code>dest</code> is not a <code>GeneralFile
   *     </code>.
   * @throws NullPointerException - If dest is null
   */
  public boolean renameTo(GeneralFile dest) throws IllegalArgumentException, NullPointerException {
    try {
      if (dest instanceof FTPFile) {
        if (ftpClient.equals(((FTPFile) dest).ftpClient)) {
          ftpClient.rename(getPath(), dest.getPath());
        } else {
          // TODO some ftp to ftp copy...

          if (!dest.exists()) {
            copyTo(dest);
            delete();
          } else return false;
        }
      } else {
        if (!dest.exists()) {
          copyTo(dest);
          delete();
        } else return false;
      }
    } catch (IOException e) {
      return false;
    } catch (FTPException e) {
      return false;
    }

    return true;
  }
Esempio n. 2
0
 /**
  * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a
  * directory, then the directory must be empty in order to be deleted.
  */
 public boolean delete() {
   try {
     if (isDirectory()) {
       ftpClient.deleteDir(getPath());
     } else {
       ftpClient.deleteFile(getPath());
     }
   } catch (IOException e) {
     return false;
   } catch (FTPException e) {
     return false;
   }
   return true;
 }
Esempio n. 3
0
 @Override
 public void connect() {
   LOGGER.info("Connecting to " + username() + "@" + host() + ":" + String.valueOf(port()));
   reconnect = true;
   try {
     ftpClient.connect(host(), port());
   } catch (IOException e) {
     throw new VirtualFileException(e);
   }
   if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
     disconnect();
     return;
   }
   login();
   setFileType(FTP.BINARY_FILE_TYPE);
 }
Esempio n. 4
0
 /**
  * Returns the length of the file denoted by this abstract pathname.
  *
  * @return The length, in bytes, of the file denoted by this abstract pathname, or <code>0L</code>
  *     if the file does not exist
  */
 public long length() {
   try {
     return ftpClient.size(getPath());
   } catch (IOException e) {
     return 0;
   } catch (FTPException e) {
     return 0;
   }
 }
Esempio n. 5
0
 /** Creates the directory named by this abstract pathname. */
 public boolean mkdir() {
   try {
     ftpClient.makeDir(getPath());
     return true;
   } catch (IOException e) {
     return false;
   } catch (FTPException e) {
     return false;
   }
 }
Esempio n. 6
0
 /**
  * Returns the time that the file denoted by this abstract pathname was last modified.
  *
  * @return A <code>long</code> value representing the time the file was last modified, measured in
  *     system-dependent way.
  */
 public long lastModified() {
   try {
     Date date = ftpClient.lastModified(getPath());
     return date.getTime();
   } catch (IOException e) {
     return 0;
   } catch (FTPException e) {
     return 0;
   }
 }
Esempio n. 7
0
 /**
  * Tests whether the file denoted by this abstract pathname exists.
  *
  * @return <code>true</code> if and only if the file denoted by this abstract pathname exists;
  *     <code>false</code> otherwise
  */
 public boolean exists() {
   try {
     ftpClient.exists(getPath());
   } catch (IOException e) {
     return false;
   } catch (FTPException e) {
     return false;
   }
   return true;
 }
Esempio n. 8
0
  /**
   * Copies this file to another file. This object is the source file. The destination file is given
   * as the argument. If the destination file, does not exist a new one will be created. Otherwise
   * the source file will be appended to the destination file. Directories will be copied
   * recursively.
   *
   * @param file The file to receive the data.
   * @throws NullPointerException If file is null.
   * @throws IOException If an IOException occurs.
   */
  public void copyTo(GeneralFile file, boolean forceOverwrite) throws IOException {
    if (file == null) {
      throw new NullPointerException();
    }

    if (isDirectory()) {
      // recursive copy
      GeneralFile[] fileList = listFiles();

      file.mkdir();
      if (fileList != null) {
        for (int i = 0; i < fileList.length; i++) {
          fileList[i].copyTo(
              FileFactory.newFile(
                  file.getFileSystem(), file.getAbsolutePath(), fileList[i].getName()),
              forceOverwrite);
        }
      }
    } else {
      if (file.isDirectory()) {
        // change the destination from a directory to a file
        file = FileFactory.newFile(file, getName());
      }
      try {
        if (file instanceof LocalFile) {
          ftpClient.get(getPath(), ((LocalFile) file).getFile());
        } else if (file instanceof FTPFile) {
          ftpClient.transfer(
              getPath(), ((FTPFile) file).getFTPClient(), file.getPath(), !forceOverwrite, null);
        } else {
          super.copyTo(file);
        }
      } catch (FTPException e) {
        IOException io = new IOException();
        io.initCause(e);
        throw io;
      }
    }
  }
Esempio n. 9
0
  /**
   * Copies this file to another file. This object is the source file. The destination file is given
   * as the argument. If the destination file, does not exist a new one will be created. Otherwise
   * the source file will be appended to the destination file. Directories will be copied
   * recursively.
   *
   * @param file The file to receive the data.
   * @throws NullPointerException If file is null.
   * @throws IOException If an IOException occurs.
   */
  public void copyFrom(GeneralFile file, boolean forceOverwrite) throws IOException {
    if (file == null) {
      throw new NullPointerException();
    }

    if (file.isDirectory()) {
      // recursive copy
      GeneralFile[] fileList = file.listFiles();

      mkdir();
      if (fileList != null) {
        for (int i = 0; i < fileList.length; i++) {
          FileFactory.newFile(this, fileList[i].getName()).copyFrom(fileList[i], forceOverwrite);
        }
      }
    } else {
      if (isDirectory()) {
        // change the destination from a directory to a file
        GeneralFile subFile = FileFactory.newFile(this, file.getName());
        subFile.copyFrom(file);
        return;
      }
      try {
        if (file instanceof LocalFile) {
          ftpClient.put(((LocalFile) file).getFile(), getPath(), !forceOverwrite);
        } else if (file instanceof FTPFile) {
          ftpClient.transfer(file.getPath(), ftpClient, getPath(), !forceOverwrite, null);
        } else {
          super.copyTo(file);
        }
      } catch (FTPException e) {
        IOException io = new IOException();
        io.initCause(e);
        throw io;
      }
    }
  }
Esempio n. 10
0
 /**
  * Returns an array of strings naming the files and directories in the directory denoted by this
  * abstract pathname.
  *
  * <p>There is no guarantee that the name strings in the resulting array will appear in any
  * specific order; they are not, in particular, guaranteed to appear in alphabetical order.
  *
  * <p>If this GeneralFile object denotes a file, the results are unspecified.
  *
  * @return An array of strings naming the files and directories in the directory denoted by this
  *     abstract pathname.
  */
 public String[] list() {
   try {
     // TODO which list?
     Vector list = ftpClient.list(getPath());
     Object[] listO = list.toArray();
     String[] listS = new String[listO.length];
     for (int i = 0; i < listO.length; i++) {
       listS[i] = listO[i].toString();
     }
     return listS;
   } catch (IOException e) {
     return null;
   } catch (FTPException e) {
     return null;
   }
 }
Esempio n. 11
0
 private void init() {
   LOGGER.debug("init");
   ftpClient = new FTPClient();
   ftpClient.setControlEncoding("UTF-8");
 }
Esempio n. 12
0
 private void checkConnection() {
   if (!ftpClient.isConnected()) connect();
 }