⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filetransferclient.java

📁 java编写的非常详尽的基于ftp协议的上传下载源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * 
     * @param localFileName    name (or full path) of the local file to be downloaded to
     * @param remoteFileName   name of the remote file to be downloaded
     * @throws FTPException 
     */
    public synchronized void downloadFile(String localFileName, String remoteFileName) throws FTPException, IOException {
        downloadFile(localFileName, remoteFileName, WriteMode.OVERWRITE);
    }
    
    /**
     * Download a file from the FTP server .
     * 
     * @param localFileName    name (or full path) of the local file to be downloaded to
     * @param remoteFileName   name of the remote file to be downloaded
     * @param writeMode        mode in which the file is written to the client machine
     * @throws FTPException 
     */
    public synchronized void downloadFile(String localFileName, String remoteFileName, WriteMode writeMode) 
        throws FTPException, IOException {
        checkTransferSettings();
        if (writeMode.equals(WriteMode.RESUME)) {
            ftpClient.resume();
        }
        else if (writeMode.equals(WriteMode.APPEND)) {
            throw new FTPException("Append not permitted for downloads");
        }
        ftpClient.get(localFileName, remoteFileName); 
    }
    
    
    /**
     * Download a file from the FTP server as a stream. The close() method <b>must</b>
     * be called on the stream once the stream has been read.
     * 
     * @param remoteFileName   name of the remote file to be downloaded
     * @return InputStream
     * @throws FTPException 
     */
     public synchronized FileTransferInputStream downloadStream(String remoteFileName) 
         throws FTPException, IOException {
         checkTransferSettings();
         return new FTPInputStream(ftpClient, remoteFileName);
    }
     
      /**
       * Upload a file to the FTP server. If a null is supplied as
       * the remoteFileName, the STOU (store unique) FTP feature will be used (if 
       * available) and the FTP server will generate a unique name for the file. 
       * 
       * @param localFileName
       *            name (or full path) of the local file to be downloaded to
       * @param remoteFileName
       *            name of the remote file to be downloaded (or null to generate a unique name)
       * @return name of remote file
       * @throws FTPException 
       */
      public synchronized String uploadFile(String localFileName, String remoteFileName) 
          throws FTPException, IOException {
          return uploadFile(localFileName, remoteFileName, WriteMode.OVERWRITE);
      }
      
      /**
       * Upload a file to the FTP server. If a null is supplied as
       * the remoteFileName, the STOU (store unique) FTP feature will be used (if 
       * available) and the FTP server will generate a unique name for the file. 
       * 
       * @param localFileName
       *            name (or full path) of the local file to be downloaded to
       * @param remoteFileName
       *            name of the remote file to be downloaded (or null to generate a unique name)
       * @param writeMode   mode to write to the remote file with
       * @return name of remote file
       * @throws FTPException 
       */
      public synchronized String uploadFile(String localFileName, String remoteFileName, WriteMode writeMode) 
          throws FTPException, IOException {  
          checkTransferSettings();
          boolean append = false;
          if (writeMode.equals(WriteMode.RESUME)) {
              ftpClient.resume();
          }
          else if (writeMode.equals(WriteMode.APPEND)) {
              append = true;
          }
          return ftpClient.put(localFileName, remoteFileName, append);
      }
      
      /**
       * Upload a file to the FTP server by writing to a stream. The close() method <b>must</b>
       * be called on the stream once the stream has been read.
       * If a null is supplied as
       * the remoteFileName, the STOU (store unique) FTP feature will be used (if 
       * available) and the FTP server will generate a unique name for the file. This
       * name can be obtained afterwards from {@see FileTransferOutputStream#getRemoteFile()}
       * 
       * @param remoteFileName   name of the remote file to be uploaded
       * @return FTPOutputStream
       * @throws FTPException 
       * @throws IOException 
       */
       public synchronized FileTransferOutputStream uploadStream(String remoteFileName) 
           throws FTPException, IOException {
           return uploadStream(remoteFileName, WriteMode.OVERWRITE);
      }

       /**
        * Upload a file to the FTP server by writing to a stream. The close() method *must*
        * be called on the stream once the stream has been read.
        * If a null is supplied as
        * the remoteFileName, the STOU (store unique) FTP feature will be used (if 
        * available) and the FTP server will generate a unique name for the file. This
        * name can be obtained afterwards from {@see FileTransferOutputStream#getRemoteFile()}
        * 
        * @param remoteFileName   name of the remote file to be uploaded
        * @param writeMode        mode for writing to the server (supporting use of append)
        * @return FTPOutputStream
        * @throws FTPException 
        * @throws IOException 
        */
        public synchronized FileTransferOutputStream uploadStream(String remoteFileName, WriteMode writeMode) 
            throws FTPException, IOException {
            checkTransferSettings();
            if (WriteMode.RESUME.equals(writeMode))
                throw new FTPException("Resume not supported for stream uploads");
            boolean append = WriteMode.APPEND.equals(writeMode);
            return new FTPOutputStream(ftpClient, remoteFileName, append);
       }
    
     /**
      * Get the size of a remote file.
      * 
      * @param remoteFileName   name of remote file
      * @return long
      * @throws FTPException 
      */
     public synchronized long getSize(String remoteFileName) 
         throws FTPException,IOException {
         return ftpClient.size(remoteFileName);
     }
     

    /**
     * Get the modified-time of a remote file.
     * 
     * @param remoteFileName   name of remote file
     * @return Date
     * @throws FTPException 
     */
    public synchronized Date getModifiedTime(String remoteFileName) throws FTPException, IOException {
        return ftpClient.modtime(remoteFileName);
    }
    
    /**
     * Set the modified-time of a remote file. May not be supported by
     * all servers.
     * 
     * @param remoteFileName   name of remote file
     * @param modifiedTime    new modified time
     * @throws FTPException 
     */
    public synchronized void setModifiedTime(String remoteFileName, Date modifiedTime) 
        throws FTPException, IOException {
        ftpClient.setModTime(remoteFileName, modifiedTime);
    }

    
    /**
     * Determine if a remote file exists.
     * 
     * @param remoteFileName   name of remote file
     * @throws FTPException 
     */
    public synchronized boolean exists(String remoteFileName) 
        throws FTPException, IOException {  
        return ftpClient.exists(remoteFileName);
    }
    
    
    /**
     * Deletes a remote file.
     * 
     * @param remoteFileName   name of remote file
     * @throws FTPException 
     * @throws IOException 
     */
    public synchronized void deleteFile(String remoteFileName) 
        throws FTPException, IOException {
        ftpClient.delete(remoteFileName);
    }

    
    /**
     * Rename a remote file or directory.
     * 
     * @param renameFromName
     *            original name
     * @param renameToName
     *            new name
      * @throws FTPException, IOException
     */
    public synchronized void rename(String renameFromName, String renameToName) 
        throws FTPException, IOException {
        ftpClient.rename(renameFromName, renameToName);
    }
    
    
    /**
     * Change directory on the FTP server. 
     * 
     * @param directoryName
     *            name the remote directory to change into
     * @throws FTPException, IOException 
     */
    public synchronized void changeDirectory(String directoryName) throws FTPException, IOException {
        ftpClient.chdir(directoryName);
    }
   
 
    /**
     * Change to parent directory on the FTP server. 
     * 
     * @throws FTPException, IOException 
     */
    public synchronized void changeToParentDirectory() throws FTPException, IOException {
        ftpClient.cdup();
    }    
    
    /**
     * Get the current remote directory.
     * 
     * @return current remote directory
     * @throws FTPException 
     * @throws IOException 
     */
    public synchronized String getRemoteDirectory() throws IOException, FTPException {
        return ftpClient.pwd();
    }
    
    /**
     * Create directory on the FTP server. 
     * 
     * @param directoryName
     *            name the remote directory to create
     * @throws FTPException, IOException 
     */
    public synchronized void createDirectory(String directoryName) throws FTPException, IOException {
        ftpClient.mkdir(directoryName);        
    }

    
    /**
     * Create directory on the FTP server. The directory must be empty. Note
     * that edtFTPj/PRO supports recursive directory deletions.
     * 
     * @param directoryName
     *            name the remote directory to create
     * @throws FTPException, IOException 
     */
    public synchronized void deleteDirectory(String directoryName) 
        throws FTPException, IOException {
        ftpClient.rmdir(directoryName);
     }

    /**
     * Disconnect from the FTP server.
     * 
     * @throws FTPException, IOException
     */
    public synchronized void disconnect() 
        throws FTPException, IOException {
        ftpClient.quit();
    }
    
    /**
     * Disconnect from the FTP server.
     * 
     * @throws FTPException, IOException
     */
    public synchronized void disconnect(boolean immediate) 
        throws FTPException, IOException {
        if (immediate)
            ftpClient.quitImmediately();
        else
            ftpClient.quit();
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -