📄 filetransferclient.java
字号:
protected void checkListingSettings() throws FTPException {
ftpClient.setParserLocales(masterContext.getParserLocales());
checkTransferSettings();
}
/**
* Get statistics on file transfers and deletions.
*
* @return FTPStatistics
*/
public synchronized FileStatistics getStatistics() {
return statistics;
}
/**
* Request that the remote server execute the literal command supplied. In
* FTP, this is the equivalent of 'quote'. It could be used to send a SITE
* command to the server, e.g. "SITE recfm=FB lrecl=180 blksize=5400".
*
* @param command command string
* @return result string returned by server
* @throws FTPException
* @throws IOException
*/
public synchronized String executeCommand(String command)
throws FTPException, IOException {
return ftpClient.quote(command);
}
/**
* Cancel current transfer if underway
*/
public void cancelAllTransfers() {
log.debug("cancelAllTransfers() called");
ftpClient.cancelTransfer();
}
/**
* Get a string that represents the remote system that the client is logged
* into.
*
* @return system string
* @throws FTPException
* @throws IOException
*/
public synchronized String getSystemType()
throws FTPException, IOException {
return ftpClient.system();
}
/**
* List the names of files and directories in the current directory on the FTP server.
*
* @return String[]
* @throws FTPException, IOException
*/
public synchronized String[] directoryNameList()
throws FTPException, IOException {
return directoryNameList("", false);
}
/**
* List the names of files and directories of a directory on the FTP server.
*
* @param directoryName name of the directory (generally not a path). Some
* servers will accept a wildcard.
* @param isLongListing true if the listing is a long format listing
* @return String[]
* @throws FTPException, IOException
*/
public synchronized String[] directoryNameList(String directoryName, boolean isLongListing)
throws FTPException, IOException {
checkListingSettings();
return ftpClient.dir(directoryName, isLongListing);
}
/**
* List the current directory on the FTP server.
*
* @throws FTPException, IOException
* @throws ParseException
*/
public synchronized FTPFile[] directoryList()
throws FTPException, IOException, ParseException {
return directoryList("");
}
/**
* List a directory on the FTP server.
*
* @param directoryName name of the directory (generally not a path). Some
* servers will accept a wildcard.
* @throws FTPException, IOException
* @throws ParseException
*/
public synchronized FTPFile[] directoryList(String directoryName)
throws FTPException, IOException, ParseException {
checkListingSettings();
return ftpClient.dirDetails(directoryName);
}
/**
* Download a file from the FTP server into a byte array.
*
* @param remoteFileName name of the remote file to be downloaded
* @throws FTPException
*/
public synchronized byte[] downloadByteArray(String remoteFileName) throws FTPException, IOException {
checkTransferSettings();
return ftpClient.get(remoteFileName);
}
/**
* 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
* @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);
}
/**
* 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 + -