这两天研究了一下 FtpClient 删除文件夹的功能,屡试不爽,发现一次性删除文件加和文件夹中的文件,删除不完全,总是要遗留一两个 没有删除的 所以没办法 采取以下方法 ,while循环
下面是实现的代码:欢迎提供好的解决方案,当前方案我不是很满意
package com.teenysoft.usermanager.util;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import sun.net.ftp.FtpClient;
public class FtpDele {
//测试
public static void main(String[] args) {
FtpDele fd = new FtpDele();
fd.deleteZt("admin4","192.168.0.142","wgr","123");
}
public void deleteZt(String ztName,String ip,String username,String pass){
//循环,返回 true 表示没有删除完全,继续删除
while(delete( ztName, ip, username, pass)){
delete( ztName, ip, username, pass);
}
}
//删除的详细代码
private boolean delete(String ztName,String ip,String username,String pass){
boolean b = true;
try{
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(ip,21);//打开连接
ftpClient.login(username,pass); //登陆FTP
ftpClient.binary();
ftpClient.serverIsOpen();
//注意红色部分是,删除完文件以后,又重新获取的ftpClient,,不然的话程序会跑死,具体原因我也不清楚,望高手指点
List<String> l = new ArrayList<String>();
deleteD(ftpClient,ztName,ztName,l);//获取要删除的所有文件夹中的文件路径
for (String s : l) {
ftpClient.sendServer("dele "+s+" \r\n");
}
ftpClient.closeServer();
ftpClient.openServer(ip,21);
ftpClient.login(username,pass);
ftpClient.binary();
ftpClient.serverIsOpen();
List<String> ll = new LinkedList<String>();
deleteDD(ftpClient,ztName,ztName,ll);//获取所有要删除的文件夹的路径
for (int i=(ll.size()-1);i>-1;i--) {
ftpClient.sendServer("xrmd "+ll.get(i)+" \r\n");
}
ftpClient.sendServer("xrmd "+ztName+" \r\n");
//看看能不能找到根目录,如果可以则表示没有删除完,继续删除,报异常表示删除完成,返回false不继续删
try{
ftpClient.cd(ztName);
}catch(Exception e){
return false;
}
ftpClient.closeServer();
}catch (Exception e) {
return true;
}
return b;
}
private void deleteD(FtpClient ftpClient,String directory,String path,List<String> ll){
try{
List<String> files = new ArrayList<String>();
ftpClient.cd(directory);
DataInputStream di=new DataInputStream(ftpClient.list());
InputStreamReader in = new InputStreamReader(di);
BufferedReader dis = new BufferedReader(in);
String filename = "";
while ((filename = dis.readLine()) != null) {
filename = filename.trim();
String s = filename.substring((filename.lastIndexOf(" ")+1),filename.length());
if(!(s.equals(".") || s.equals(".."))){
files.add(s);
}
}
for(int i=0;i<files.size();i++){
String fi = files.get(i);
//暂时使用“.”的方式来判断是不是目录
if(!(fi.indexOf(".")==-1)){
ll.add(path + File.separator + fi);
}else{
String p2 = path + File.separator + fi;
deleteD(ftpClient,fi,p2,ll);
}
}
ftpClient.cd("..");
}catch (Exception e) {
e.getStackTrace();
}
}
private void deleteDD(FtpClient ftpClient,String directory,String path,List<String> ll) throws Exception{
try{
List<String> files = new ArrayList<String>();
ftpClient.cd(directory);
DataInputStream di=new DataInputStream(ftpClient.list());
InputStreamReader in = new InputStreamReader(di);
BufferedReader dis = new BufferedReader(in);
String filename = "";
while ((filename = dis.readLine()) != null) {
filename = filename.trim();
String s = filename.substring((filename.lastIndexOf(" ")+1),filename.length());
if(!(s.equals(".") || s.equals(".."))){
files.add(s);
}
}
for(int i=0;i<files.size();i++){
String fi = files.get(i);
//暂时使用“.”的方式来判断是不是目录
if(fi.indexOf(".")==-1){
ll.add(path + File.separator + fi);
String p2 = path + File.separator + fi;
deleteDD(ftpClient,fi,p2,ll);
}
}
ftpClient.cd("..");
}catch (Exception e) {
e.getStackTrace();
}
}
}