永发信息网

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

答案:2  悬赏:60  手机版
解决时间 2021-04-26 22:07
java FTPClient如何删除远程服务器端的文件夹及其子文件夹及其内容!
最佳答案
//遍历文件
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的没看过
全部回答
楼上说了遍历文件夹底下所有文件的方法
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;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
烽火战国怎么查到自已在哪个方位
文和友的臭豆腐有那么好吃吗
买了个电箱的尤克里里 怎么用呐
谁听过华范速热式电热水器,可以介绍一下吗?
魔兽世界玩啥职业好
问:📢📢刚买的小蚁智
LOL英雄联盟齐天大圣怎么玩?我是一个新手,刚
java与jsp有什么区别?jsp+Ajax开发有什么优
怎样区别典压汽车
老公每天晚上吸我奶能吸吗
有没有比较污的网名
制定的现规定试行是否可以不执行
今天有了一点蛋清状的液体排出了,是今晚同房
为什么电脑和手机进行qq视频时,电脑方看不到
填一填.(1)16时30分是下午______时______
推荐资讯
谁能帮我练DNF号,现在49级,练到55级
玉器吊牌后面的数字代表什么意思
用手机使用java游戏程序请问要流量还是另外收
DNF三十级以前有什么称号?
诺基亚6120c好吗
为什么会有感情这种东西?
N73_为什么会卡、卡到死机、我安装的软件不是
请娱乐节目别在侮辱家驹了行吗
今年回没回克过年自己还门懂!都是钱惹滴祸!诺
韩国有首歌很红,是一个女子组合唱的,好像叫
是不是把对方拉进黑名单,对方就没有你的Q?
老班快结婚了,应该送什么?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?