本文整理匯總了Java中org.apache.commons.net.ftp.FTPClient.deleteFile方法的典型用法代碼示例。如果您正苦於以下問題:Java FTPClient.deleteFile方法的具體用法?Java FTPClient.deleteFile怎麽用?Java FTPClient.deleteFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.deleteFile方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: deleteFile
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 刪除文件
*
* @author wangwei
*/
public Map<String,String> deleteFile(String filename, String remoteFolder)
throws Exception {
Map<String,String> rs = new HashMap<String, String>();
// 連接FTP服務器
FTPClient ftp = this.connectFTPServer();
try {
// 改變當前路徑到指定路徑
this.changeDirectory(remoteFolder);
boolean result = ftp.deleteFile(filename);
if (!result) {
throw new Exception("FTP刪除文件失敗!");
}else{
rs.put("ret", "success");
}
} catch (Exception e) {
throw e;
}
return rs;
}
示例2: delete
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* Convenience method, so that we don't open a new connection when using this
* method from within another method. Otherwise every API invocation incurs
* the overhead of opening/closing a TCP connection.
*/
private boolean delete(FTPClient client, Path file, boolean recursive)
throws IOException {
Path workDir = new Path(client.printWorkingDirectory());
Path absolute = makeAbsolute(workDir, file);
String pathName = absolute.toUri().getPath();
try {
FileStatus fileStat = getFileStatus(client, absolute);
if (fileStat.isFile()) {
return client.deleteFile(pathName);
}
} catch (FileNotFoundException e) {
//the file is not there
return false;
}
FileStatus[] dirEntries = listStatus(client, absolute);
if (dirEntries != null && dirEntries.length > 0 && !(recursive)) {
throw new IOException("Directory: " + file + " is not empty.");
}
for (FileStatus dirEntry : dirEntries) {
delete(client, new Path(absolute, dirEntry.getPath()), recursive);
}
return client.removeDirectory(pathName);
}
示例3: deleteFile
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 刪除服務器上指定的文件
*
* @author gaoxianglong
*/
public boolean deleteFile(File file) {
boolean result = false;
FTPClient ftpClient = ftpConnManager.getFTPClient();
if (null == ftpClient || !ftpClient.isConnected()) {
return result;
}
try {
result = ftpClient.deleteFile(file.getName());
if (result) {
result = true;
log.info("file-->" + file.getPath() + "成功從FTP服務器刪除");
}
} catch (Exception e) {
log.error("error", e);
} finally {
disconnect(ftpClient);
}
return result;
}
示例4: delete
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 刪除文件
*
* @param remoteDir 操作目錄
* @param remoteFileName 刪除文件名
* @return 刪除結果<br> true - 刪除成功<br>
* false - 刪除失敗
*/
public boolean delete(String remoteDir, String remoteFileName) {
FTPClient ftp = null;
try {
ftp = initFtpClient(remoteDir);
if (ftp == null) {
logger.debug("ftp初始化失敗");
return false;
}
boolean deleteRet = ftp.deleteFile(remoteFileName);
if (!deleteRet) {
logger.debug("刪除文件失敗");
return false;
} else {
logger.debug("刪除文件成功");
}
return true;
} catch (IOException e) {
logger.error("FTP操作異常", e);
return false;
} finally {
close(ftp);
}
}
示例5: delete
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
@Override
public void delete() throws SocketException, IOException, AuthenticationException {
FTPClient ftp = null;
if (mUri.getScheme().equals("ftps"))
ftp = Session.getInstance().getNewFTPSClient(mUri, -1);
else
ftp = Session.getInstance().getNewFTPClient(mUri, -1);
ftp.deleteFile(mUri.getPath());
}
示例6: deleteFile
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
@Override
public void deleteFile(final String path, final String remoteFileName) throws IOException {
final FTPClient client = getClient(null);
if (path != null) {
setWorkingDirectory(path);
}
if (!client.deleteFile(remoteFileName)) {
throw new IOException("Failed to remove file " + remoteFileName + " due to " + client.getReplyString());
}
}
示例7: deleteDir
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 刪除文件夾下所有的文件(非文件夾)
*
* @param fullDir 待刪除目錄完整ftp路徑
* @return 刪除結果<br> true - 刪除成功<br>
* false - 刪除失敗
*/
public boolean deleteDir(String fullDir) {
FTPClient ftp = null;
// fullDir = encode(fullDir);
try {
ftp = initFtpClient(fullDir);
if (ftp == null) {
logger.debug("ftp初始化失敗");
return false;
}
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile ftpFile : ftpFiles) {
if (!ftpFile.isDirectory()) {
boolean deleteRet = ftp.deleteFile(ftpFile.getName());
if (!deleteRet) {
logger.debug("刪除文件失敗");
return false;
}
}
}
}
return true;
} catch (IOException e) {
logger.error("FTP操作異常", e);
return false;
} finally {
close(ftp);
}
}
示例8: testFtpQuotaAndFtp
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* Test a quota failue exception over FTP.
* A file should not exist after a create and quota exception.
*/
public void testFtpQuotaAndFtp() throws Exception
{
// Enable usages
ContentUsageImpl contentUsage = (ContentUsageImpl)applicationContext.getBean("contentUsageImpl");
contentUsage.setEnabled(true);
contentUsage.init();
UserUsageTrackingComponent userUsageTrackingComponent = (UserUsageTrackingComponent)applicationContext.getBean("userUsageTrackingComponent");
userUsageTrackingComponent.setEnabled(true);
userUsageTrackingComponent.bootstrapInternal();
final String TEST_DIR="/Alfresco/User Homes/" + USER_THREE;
FTPClient ftpOne = connectClient();
try
{
int reply = ftpOne.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftpOne.login(USER_THREE, PASSWORD_THREE);
assertTrue("user three login not successful", login);
boolean success = ftpOne.changeWorkingDirectory("Alfresco");
assertTrue("user three unable to cd to Alfreco", success);
success = ftpOne.changeWorkingDirectory("User*Homes");
assertTrue("user one unable to cd to User*Homes", success);
success = ftpOne.changeWorkingDirectory(USER_THREE);
assertTrue("user one unable to cd to " + USER_THREE, success);
/**
* Create a file as user three which is bigger than the quota
*/
String FILE3_CONTENT_3="test file 3 content that needs to be greater than 100 bytes to result in a quota exception being thrown";
String FILE1_NAME = "test.docx";
// Should not be success
success = ftpOne.appendFile(FILE1_NAME , new ByteArrayInputStream(FILE3_CONTENT_3.getBytes("UTF-8")));
assertFalse("user one can ignore quota", success);
boolean deleted = ftpOne.deleteFile(FILE1_NAME);
assertFalse("quota exception expected", deleted);
logger.debug("test done");
}
finally
{
// Disable usages
contentUsage.setEnabled(false);
contentUsage.init();
userUsageTrackingComponent.setEnabled(false);
userUsageTrackingComponent.bootstrapInternal();
ftpOne.dele(TEST_DIR);
if(ftpOne != null)
{
ftpOne.disconnect();
}
}
}
示例9: upload
import org.apache.commons.net.ftp.FTPClient; //導入方法依賴的package包/類
/**
* 上傳文件到FTP服務器,支持斷點續傳
*
* @param localFile 本地文件
* @param remoteFilePath 遠程文件路徑,使用/home/directory1/subdirectory/file.ext
* 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
* @return 上傳結果
* @throws IOException
*/
public UploadStatus upload(FTPClient ftpClient, File localFile, String remoteFilePath) throws IOException {
// 設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
// 設置以二進製流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
UploadStatus result;
// 對遠程目錄的處理
String remoteFileName = remoteFilePath;
if (remoteFilePath.contains("/")) {
remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
// 創建服務器遠程目錄結構,創建失敗直接返回
if (createDirecroty(remoteFilePath, ftpClient) == UploadStatus.Create_Directory_Fail) {
return UploadStatus.Create_Directory_Fail;
}
}
// 檢查遠程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET));
if (files.length == 1) {
long remoteSize = files[0].getSize();
// File f = new File(localFilePath)
long localSize = localFile.length();
if (remoteSize == localSize) { // 文件存在
return UploadStatus.File_Exits;
} else if (remoteSize > localSize) {
return UploadStatus.Remote_Bigger_Local;
}
// 嘗試移動文件內讀取指針,實現斷點續傳
result = uploadFile(remoteFileName, localFile, ftpClient, remoteSize);
// 如果斷點續傳沒有成功,則刪除服務器上文件,重新上傳
if (result == UploadStatus.Upload_From_Break_Failed) {
if (!ftpClient.deleteFile(remoteFileName)) {
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
} else {
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
return result;
}