A. 求一段可以读取FTP一个文件夹下面当天更新数据的文件 ,c#写的代码
#region 下载文件
/**//// <summary>
/// 从FTP服务器下载文件,使用与远程文件同名的文件名来保存文件
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
public bool DownloadFile(string RemoteFileName, string LocalPath)
{
return DownloadFile(RemoteFileName, LocalPath, RemoteFileName);
}
/**//// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
/// <param name="LocalPath">本地路径</param>
/// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
/// <param name="LocalFileName">保存本地的文件名</param>
public bool DownloadFile(string RemoteFileName, string LocalPath, string LocalFileName)
{
byte[] bt = null;
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(LocalFileName) || !IsValidPathChars(LocalPath))
{
throw new Exception("非法文件名或目录名!");
}
if (!Directory.Exists(LocalPath))
{
throw new Exception("本地文件路径不存在!");
}
string LocalFullPath = Path.Combine(LocalPath, LocalFileName);
if (File.Exists(LocalFullPath))
{
throw new Exception("当前路径下已经存在同名文件!");
}
bt = DownloadFile(RemoteFileName);
if (bt != null)
{
FileStream stream = new FileStream(LocalFullPath, FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
/**//// <summary>
/// 从FTP服务器下载文件,返回文件二进制数据
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
public byte[] DownloadFile(string RemoteFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName))
{
throw new Exception("非法文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DownloadFile);
Stream Reader = Response.GetResponseStream();
MemoryStream mem = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = Reader.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
mem.Write(buffer, 0, bytesRead);
}
if (mem.Length > 0)
{
return mem.ToArray();
}
else
{
return null;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
B. 如何使用批处理从FTP服务器上下载当天创建的所有文件
FTP命令好象不能那么筛选,不过你可以在DATE下建立一个新文件夹,把所有文件都放进去,然后DATE下就只有每天创建的文件文件,下载下来后在移动过去,这样不就简单了
然后你可以每天用下面的脚本来下载文件
@echo
off
echo
open
192.168.1.1
>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
user
>>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
user
>>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
cd
date
>>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
binary
>>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
mget
*.*
c:\date
>>
"%userprofile%\Local
Settings\Temp\ftpin"
echo
bye
>>
"%userprofile%\Local
Settings\Temp\ftpin"
ftp
-s:"%userprofile%\Local
Settings\Temp\ftpin"
del
"%userprofile%\Local
Settings\Temp\ftpin"
C. 在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地址。此参数必须放到最后。
D. ftp目录设置为只读(设置后该目录无法更改,删除,添加文件,只允许读取。),对SEO有影响吗
不会有影响的,只读只是对用户操作来说有影响,对搜索引擎抓取是没有任何影响的。
E. 搭建的FTP服务器,匿名用户只能查看文件列表,却无开打开文档和文件夹
你是用微软的IIS服务中的FTP吗?建议你用serverU。
你测试你的FTP服务时,移动文件夹是需要建立和删除权限的,你赋予你的匿名用户了吗?另外
记得刷新你的浏览器。多尝试是最好的老师。相信你会搭建成的!不只是匿名用户包括授权用户。
F. FTP怎么读取一个特定目录文件
楼主您好,您可以把ftp用户的家目录设置为你需要读取的目录,然后不然其跳出自己的家目录。这样您一连接ftp就进入您需要读取的目录了。
G. 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;
}
H. 用JAVA获取FTP文件列表
学习一下ftp协议,然后用socket来模拟就可以了
I. 如何使用批处理从FTP服务器上下载当天创建的所有文件夹
ftp>
open
123.123.123.123
ftp>
connected
to
接下来服务器询问你用户名和口令
ftp>
cd
wwwroot
ftp>
get
123
也可以在ie地址栏输入ftp地址,输入账号和密码,然后就可以访问了
J. 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
满意就采纳 谢谢