本文整理汇总了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;
}