java FTPClient如何删除远程服务器端的文件夹及其子文件夹及其内容!

请注意是文件夹的内容,不是文件。所用的包是sun.net.ftp.FtpClient!我需要明确的知道如何便利的到文件夹下的文件。问题解决分数可以再加!
我需要知道的是如何获取远程文件夹下的文件夹列表?

假如文件夹里面有文件的话,ftpclient根本删除不了文件夹,不像其他api可以自动递归删除,所以得先删除文件夹里面的文件,然后在删除文件夹,
删除之前记得改变下工作目录 fileName是dirName里面的文件
ftpClient.changeWorkingDirectory(remoteDir+dirName)

删除文件命令:ftpClient.deleteFile(fileName);
删除完文件后更改目录ftpClient.changeWorkingDirectory(remoteDir)
删除文件夹命令:ftpClient.removeDirectory(dirName);
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-10
楼上说了遍历文件夹底下所有文件的方法

ftpClient所带的API提供了

ftpClient.sendServer("DELE " + filename + "\r\n");

但是它没有返回值,所以在实际应用中它还是有时候存在删不掉的问题。

贴一段代码不知道是不是你想要的。

import sun.net.ftp.*;
import java.io.*;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.*;
import java.text.DecimalFormat;

//FtpClass类
class FtpClass {
FtpClient client;
private String host;
private String username;
private String password;
private String path = "/";
private int port = 21;
private static FtpClass instance;

private FtpClass() {
}

// 获得唯一实例

public static FtpClass getInstance() {
if (instance == null) {
instance = new FtpClass();
}
return instance;
}

// 连接FTP,并进入当前的path

public void connect() throws IOException {
if (client == null) {
client = new FtpClient(host, port);
client.login(username, password);
client.binary();
client.cd(path);
}
else {
client.noop();
client.cd(path);
}
}

// 关闭FTP

public void close() throws IOException {
if (client != null) {
client.closeServer();
client=null;
}
}

// 获得FTPClient类,可以直接对其操作

public FtpClient getClient() throws IOException {
if (client == null) {
connect();
}
return client;
}

// 返回当前目录的所有文件及文件夹

public ArrayList getFileList() throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(client.list()));
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) {
if ((!((String) parseLine(s).get(8)).equals("."))&&(!((String) parseLine(s).get(8)).equals("..")))
al.add(s);
}
return al;
}

// 返回当前目录的文件名称

public ArrayList getNameList() throws IOException {
BufferedReader dr = new BufferedReader(new InputStreamReader(client.nameList(path)));
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) {
al.add(s);
}
return al;
}

// 判断一行文件信息是否为目录

public boolean isDir(String line) {
return ( (String) parseLine(line).get(0)).indexOf("d") != -1;
}

public boolean isFile(String line) {
return!isDir(line);
}

// 处理getFileList取得的行信息

private ArrayList parseLine(String line) {
ArrayList s1 = new ArrayList();
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
s1.add(st.nextToken());
}
return s1;
}

public String getFileName(String line) {
int i;
String filename=(String) parseLine(line).get(8);
for (i=9;i {
filename=filename+" "+((String) parseLine(line).get(i));
}
return filename;
}

public String getFileSize(String line) {
return (String) parseLine(line).get(4);
}

public String getFileDate(String line) {
ArrayList a = parseLine(line);
return (String) a.get(5) + " " + (String) a.get(6) + " " + (String) a.get(7);
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPath() {
return path;
}

public void setPath(String path) throws IOException {
if (client == null)
this.path = path;
else {
client.cd(path);
}
}

public void setPort(int port) {
this.port = port;
}

}本回答被提问者采纳
第2个回答  推荐于2018-03-13
//遍历文件
public List getFileList(FtpClient ftpClient, String serverPath,
String prefix, String suffix) throws Exception {
List list = new ArrayList();

DataInputStream dis = new DataInputStream(ftpClient
.nameList(serverPath));
String filename = "";
while ((filename = dis.readLine()) != null) {

list.add(filename);

}

return list;
}

//删文件
public void deleteFileFromFtp(FtpClient ftpClient, String delFileName,
String serverPath) throws Exception {

ftpClient.cd(serverPath);
ftpClient.sendServer("dele " + delFileName + "\r\n");

}

删文件夹的话就先遍历再删

文件夹列表用apache提供的ftp包可以,sun的没看过本回答被网友采纳