DataInputStream dis = new DataInputStream(ftpClient.nameList(path)); String filename = ""; while((filename=dis.readLine())!=null)
{
System.out.println(filename); // 打印文件或文件夹的名称
//问题是我如何判断filename是文件还是文件夹呢?
}
解决方案 »
- jsp页面不显示服务器端返回的错误信息
- 用spring来管理Hibernate的时候,是如何开关数据库连接的
- native2ascii中文默认转换后是什么编码
- 在对象注册表中存储和获得远程对远程对象引用的方法
- 求助,循环向div中插入数据出错
- jsp中写查询
- 登录到一个框架中怎么显示
- 我用SmartUpload类做的servlet下载,速度巨慢,而且总是同一个文件。
- 高手请回答!
- 请问,winXP没有了内建的java虚拟机,是不是就意味着winXP下面不可以浏览JSP网页了???请知道的告诉我,谢谢,我在想还有没有必要学习JSP.......
- MyEclipse 5.5连接sql数据库怎么连
- java.sql.Date时间处理问题
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,并进入当前的pathpublic 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);
}
}// 关闭FTPpublic 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 boolean isDir(String line) {
return ( (String) parseLine(line).get(0)).indexOf("d") != -1;
} public boolean isFile(String line) {
return!isDir(line);
}
--------------------------------------------------------------------
不行呀,不过我想了一个另外的方法。现在我把问题换一个吧。哪位用过sun.net.ftp.FtpClient包来上传下载文件?我现在用这个包,但是有个问题:
ftpClient.sendServer("DELE /1.txt\r\n");
这句是用来删除文件的。sendServer是调用系统ftp命令的。但是当我联系删除两个文件时:
ftpClient.sendServer("DELE /a.txt\r\n");
ftpClient.sendServer("DELE /b.txt\r\n");在删除第二个时就会出错:
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT |1|192.168.5.44|4628|': command not understood
Thread.sleep(50);
System.out.println(filename);
睡一下 就可以了