java FTPClient如何删除远程服务器端的文件夹及其子文件夹及其内容!
请注意是文件夹的内容,不是文件。所用的包是sun.net.ftp.FtpClient!我需要明确的知道如何便利的到文件夹下的文件。问题解决分数可以再加!我需要知道的是如何...
请注意是文件夹的内容,不是文件。所用的包是sun.net.ftp.FtpClient!我需要明确的知道如何便利的到文件夹下的文件。问题解决分数可以再加!
我需要知道的是如何获取远程文件夹下的文件夹列表? 展开
我需要知道的是如何获取远程文件夹下的文件夹列表? 展开
展开全部
假如文件夹里面有文件的话,ftpclient根本删除不了文件夹,不像其他api可以自动递归删除,所以得先删除文件夹里面的文件,然后在删除文件夹,
删除之前记得改变下工作目录 fileName是dirName里面的文件
ftpClient.changeWorkingDirectory(remoteDir+dirName)
删除文件命令:ftpClient.deleteFile(fileName);
删除完文件后更改目录ftpClient.changeWorkingDirectory(remoteDir)
删除文件夹命令:ftpClient.removeDirectory(dirName);
删除之前记得改变下工作目录 fileName是dirName里面的文件
ftpClient.changeWorkingDirectory(remoteDir+dirName)
删除文件命令:ftpClient.deleteFile(fileName);
删除完文件后更改目录ftpClient.changeWorkingDirectory(remoteDir)
删除文件夹命令:ftpClient.removeDirectory(dirName);
快又稳
2024-10-28 广告
2024-10-28 广告
在Linux环境下配置基于域名的虚拟主机,需安装Apache或Nginx等Web服务器,并编辑配置文件。以Apache为例,需创建虚拟主机配置文件,指定域名、文档根目录等,然后启用该配置文件并重启Apache服务。同样,Nginx也需在相应...
点击进入详情页
本回答由快又稳提供
展开全部
楼上说了遍历文件夹底下所有文件的方法
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;
}
}
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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//遍历文件
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的没看过
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的没看过
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |