1. java如何获取ftp制定目录下所有文件集合(包括文件名称)只要一个方法。
/**
* 取得相对于当前连接目录的某个目录下所有文件列表
*
* @param path
* @return
*/
public List getFileList(String path){
List list = new ArrayList();
DataInputStream dis;
try {
dis = new DataInputStream(ftpClient.nameList(this.path + path));
String filename = "";
while((filename = dis.readLine()) != null){
list.add(filename);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
我从这里拷来的 你不清楚看看里面 http://hi..com/yuanhotel/item/000b6334894d11f42784f4da
满意就采纳 谢谢
2. java 怎么遍历ftp目录下的所有目录以及目录下的文件名称,取出文件的相对路径
package com.hmilyld.exp;
import java.io.File;
public class ListFile {
private long[] count = new long[] { 0, 0 };
private File file;
private long[] listFile(String path) {
file = new File(path);
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
count[0]++;
this.listFile(f[i].getPath());
} else {
count[1]++;
}
}
return count;
}
/**
* 得到指定路径下的文件和文件夹数量
*
* @param path
* 要查看的路径
* @return object[0]耗时(毫秒)<br>
* object[1]文件夹数量<br>
* object[2]文件数量
*/
public Object[] getFileCount(String path) {
long t = System.currentTimeMillis();
long[] count = this.listFile(path);
t = System.currentTimeMillis() - t;
Object[] o = new Object[] { Long.valueOf(t), Long.valueOf(count[0]),
Long.valueOf(count[1])};
return o;
}
public static void main(String[] args) {
ListFile l = new ListFile();
Object[] count = l.getFileCount("d:\\");
System.out.println(count[0]);
System.out.println(count[1]);
System.out.println(count[2]);
}
}
以前写的一个获取目录下有多少文件和多少文件夹的代码,
可以参考下.:)
3. 在FTP命令当中查看本地文件列表命令是
查看本地文件列表命令是:ls
其它常用的FTP命令及含义:
1、dir:显示服务器目录和文件列表
2、cd:进入服务器指定的目录(dir命令可以使用通配符“”和“?”,比如,显示当前目录中所有扩展名为jpg的文件,可使用命令 dir .jpg。)
3、put:上传指定文件put filename [newname]
4、、send:上传指定文件send filename [newname]
(filename为上传的本地文件名,newname为上传至FTP服务器上时使用的名字,如果不指定newname,文件将以原名上传。)
(3)ftp获取目录下文件数量扩展阅读
ftp命令行格式及开关含义:
ftp [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [computer]
-v - 禁止显示远程服务器相应信息
-n - 禁止自动登录
-i - 多文件传输过程中关闭交互提示
-d - 启用调试,显示所有客户端与服务器端传递的命令
-g - 禁用文件名通配符,允许在本地文件和路径名中使用
-s:filename - 指定包含 FTP 命令的文本文件;命令在FTP启动后自动运行。此参数中没有空格。可替代重定向符(>)使用。
-a - 在绑字数据连接时使用所有本地接口
-w:windowsize - 覆盖默认的传输缓冲区大小 65535。
computer - 指定远程电脑计算机名或IP地址。此参数必须放到最后。
4. windowsftp如何获取文件夹下所有的文件
可以通过命令窗口来打开所有的文件。具体步骤如下:
点击win+R后输入cmd打开dos命令窗口。
打开需获取文件名的位置。
获取名称,命令格式:dir/b文件目标盘符文件夹位置(可省略)目标名称.目标后缀。
获取文件大小及文件名、修改时间(文件大小需处理)。
Microsoft Surface是一个由微软所开发的第一款平面电脑,结合硬件与软件的新技术,用家可以直接用手或声音对屏幕作出指令,触摸和其他外在物理物来和电脑进行交互,毋须再依赖会令手部劳损的鼠标与键盘。
5. FTP 怎么查看本地的文件列表
一个简单的问题,ftp到远程服务器之后需要上传一个文件,但是我忘记了本地需要上传的文件名,因此需要查看本地目录。
解决方法:
一、cd到本地目录
二、ls命令查看本地目录的内容。
6. C# 获取Ftp某个目录下的所有文件(不要文件夹)
我在之前做过一个FTP的客户端工具。
drw 文件夹
-rw 文件(有扩展名或无扩展名)
我是根据服务端返回的报文进行分析获取的列表。
给你一些代码片段:
/// <summary>
/// 获取指定目录下的文件和文件夹。
/// </summary>
/// <param name="path">要获取的目录</param>
/// <param name="WRMethods">要发送到FTP服务器的密令。</param>
/// <returns></returns>
public string[] GetFileList(string path, string WRMethods)//从ftp服务器上获得文件列表
{
WebResponse response;
string[] downloadFiles;
int conut = 4;
StringBuilder result = new StringBuilder();
Connect(path);
if (FTPVariable.IsUseProxy_ftp)
{
reqFTP.Proxy = FtpProxy.GetFtpSelectProxy(FTPVariable.FtpCommand_transferProxyName);
}
reqFTP.ReadWriteTimeout = 12000;
//如果不应销毁到服务器的连接,则为 true;否则为 false。默认值为 true。
//
reqFTP.Method = WRMethods;
try
{
response = (FtpWebResponse)reqFTP.GetResponse();
goto Ftp_lbl_03;
}
catch (WebException webex)
{
GetReply(webex.Message);
if (ReplyCode == 530)// 未登录。
{
goto Ftp_lbl_04;
}
else if (ReplyCode == 550)
{
goto Ftp_lbl_04;
}
else
{
FtpManage.SetLog("获取列表超时,等候1秒后重试!");
goto Ftp_lbl_01;
}
}
Ftp_lbl_01:
try
{
FtpManage.SetLog("正在连接服务器 " + FtpRemoteHost);
response = GetRequest(path, WRMethods);
}
catch (WebException)
{
FtpManage.SetLog("获取列表超时,等候1秒后重试!");
downloadFiles = null;
System.Threading.Thread.Sleep(1000);
if (conut == 0)
{
goto Ftp_lbl_02;
}
conut--;
goto Ftp_lbl_01;
}
catch (Exception ex)
{
MSG.Show(ex.Message, Global.GetRS["msgTilteError"], MessageBoxButton.OK, MsgIco.Error);
FtpManage.SetLog("命令执行失败,原因:" + ex.Message);
downloadFiles = null;
return downloadFiles;
}
Ftp_lbl_03:
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
if (result.Length == 0)
{
return null;
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
FtpManage.SetLog("命令已成功执行");
return result.ToString().Split('\n');
Ftp_lbl_04:
FtpManage.SetLog(ReplyInfo);
return null;
Ftp_lbl_02:
FtpManage.SetLog("550 获取列表失败,无法连接远程服务器!");
FtpManage.ftpmanage.IsRefurbish = true;
return null;
}
/// <summary>
/// 获取指定目录下的文件和文件夹。
/// </summary>
/// <param name="path">要获取的目录</param>
/// <returns></returns>
public string[] GetFileList(string path)//从ftp服务器上获得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectory);
}
/// <summary>
/// 获取指定目录下的文件和文件夹。
/// </summary>
/// <returns></returns>
public string[] GetFileList()//从ftp服务器上获得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/", WebRequestMethods.Ftp.ListDirectory);
}
/// <summary>
/// 获取目录和文件名,返回目录表。
/// </summary>
/// <param name="path">要获取的目录</param>
/// <returns></returns>
public string[] GetCatalog_FileList(string path)
{
string[] fountainhead = GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);
string[] Catalog = null;
if (fountainhead == null)
{
return null;
}
Catalog = new string[fountainhead.Length];
for (int i = 3; i < fountainhead.Length; i++)
{
Catalog[i - 3] += fountainhead[i].Substring(55, fountainhead[i].Length - 55) + "&";//FileName
Catalog[i - 3] += fountainhead[i].Substring(30, 12) + "&";//FileSize
Catalog[i - 3] += fountainhead[i].Substring(42, 13) + "&";//AmendDate
Catalog[i - 3] += fountainhead[i].Substring(0, 3) + "&";
}
return Catalog;
}
7. linux下ftp到另一台服务器,如何查看服务器上的某个目录下的文件个数:
您登陆到ftp上用ls |wc -l就可以,直接列出有多少个文件目录,我记得在57CTO技术论坛看到过。