您的位置:首页 > 编程语言 > ASP

asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类

2013-01-18 17:22 861 查看

asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类

对于asp.net,默认只允许上传2M文件 fileupload的扩展

对于asp.net,默认只允许上传2M文件,在<system.web>增加如下配置,一般可以自定义最大文件大小.

<httpRuntime

executionTimeout="300"

maxRequestLength="40960"

useFullyQualifiedRedirectUrl="false"/>

另外: 判断客户端文件大小

this.FileUpload1.PostedFile.ContentLength;

单位为字节KB

参数最大是2091151,2G左右大小

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Net;

using System.IO;

using System.Web.Configuration;

namespace Webftp

{

public sealed class FtpClientService

{

#region Internal Members

private NetworkCredential certificate;

#endregion

/// <summary>

/// 构造函数,提供初始化数据的功能,打开Ftp站点

/// </summary>

public FtpClientService()

{

certificate = new NetworkCredential(WebConfigurationManager.AppSettings["FtpUserName"], WebConfigurationManager.AppSettings["FtpPassword"]);

}

/// <summary>

/// 创建FTP请求

/// </summary>

/// <param name="uri">ftp://myserver/upload.txt</param>

/// <param name="method">Upload/Download</param>

/// <returns></returns>

private FtpWebRequest CreateFtpWebRequest(Uri uri, string method)

{

FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri);

ftpClientRequest.Proxy = null;

ftpClientRequest.Credentials = certificate;

ftpClientRequest.KeepAlive = true;

ftpClientRequest.UseBinary = true;

ftpClientRequest.UsePassive = true;

ftpClientRequest.Method = method;

ftpClientRequest.UsePassive = true;

//ftpClientRequest.Timeout = -1;

return ftpClientRequest;

}

#region 支持断点续传

public bool UploadFile(string sourceFile, Uri destinationPath, int offSet, string ftpMethod)

{

try

{

FileInfo file = new FileInfo(sourceFile);

Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name);

FtpWebRequest request = CreateFtpWebRequest(uri, ftpMethod);

request.ContentOffset = offSet;

Stream requestStream = request.GetRequestStream();//需要获取文件的流

FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);//创建存储文件的流

int sourceLength = (int)fileStream.Length;

offSet = CopyDataToDestination(fileStream, requestStream, offSet);

WebResponse response = request.GetResponse();

response.Close();

if (offSet != 0)

{

UploadFile(sourceFile, destinationPath, offSet, WebRequestMethods.Ftp.AppendFile);

}

}

catch (Exception ex)

{

return false;

}

return true;

}

private int CopyDataToDestination(Stream sourceStream, Stream destinationStream, int offSet)

{

try

{

int sourceLength = (int)sourceStream.Length;

int length = sourceLength - offSet;

byte[] buffer = new byte[length + offSet];

int bytesRead = sourceStream.Read(buffer, offSet, length);

while (bytesRead != 0)

{

destinationStream.Write(buffer, 0, bytesRead);

bytesRead = sourceStream.Read(buffer, 0, length);

length = length - bytesRead;

offSet = (bytesRead == 0) ? 0 : (sourceLength - length);//(length - bytesRead);

}

}

catch (Exception ex)

{

string error = ex.ToString();

return offSet;

}

finally

{

destinationStream.Close();

sourceStream.Close();

}

return offSet;

}

#endregion

/// <summary>

/// 获取Ftp服务器上的文件列表 注意,不包括文件夹(目录)

/// </summary>

/// <param name="serverAddress">服务器地址 例如">

/// <param name="user">帐号</param>

/// <param name="pwd">密码</param>

/// <returns>文件列表字符串</returns>

public string[] GetFileList(string serverAddress, string user, string pwd)

{

//创建Web发送请求对象

WebRequest wreq = WebRequest.Create(new Uri(serverAddress));

//创建网络凭证

wreq.Credentials = new NetworkCredential(user, pwd);

//设置请求中所使用的协议

wreq.Method = WebRequestMethods.Ftp.ListDirectory;

//设置请求中文件是大小

//wreq.ContentLength = inputStream.Length;

//实例化输出文件流

StreamReader sr = new StreamReader(wreq.GetResponse().GetResponseStream());

//定义字符串,连接Ftp文件名

StringBuilder str = new StringBuilder();

//定义字符串,获取其中一行字符串

string lineStr = sr.ReadLine();

//判断是否为空

if (lineStr != null)

{

str.Append(lineStr);

lineStr = sr.ReadLine();

while (lineStr != null)

{

str.Append("-" + lineStr);

lineStr = sr.ReadLine();

}

}

sr.Close();

return str.ToString().Split('-');

}

}

//删除文件

public bool DeleteFileName(string deletefile, Uri destinationPath)

{

try

{

FileInfo file = new FileInfo(deletefile);

Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name);

FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.DeleteFile);

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

return true;

}

catch (Exception ex)

{

return false;

}

}

//创建目录

public bool MakeDir(string dirName, Uri destinationPath)

{

FtpWebResponse response = null;

try

{

FileInfo file = new FileInfo(dirName);

Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name);

FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.MakeDirectory);

response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

return true;

}

catch (Exception ex)

{

return false;

}

}

//删除目录

public bool DeleteDir(string dirName, Uri destinationPath)

{

FtpWebResponse response = null;

try

{

FileInfo file = new FileInfo(dirName);

Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name);

FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.RemoveDirectory);

response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

return true;

}

catch (Exception ex)

{

return false;

}

}

//文件改名

public bool Rename(string currentFilename, string newFilename, Uri destinationPath)

{

try

{

FileInfo fileInf = new FileInfo(currentFilename);

Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + fileInf.Name);

FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.Rename);

reqFTP.RenameTo = newFilename;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

return true;

}

catch (Exception ex)

{

return false;

}

}

//获得文件

public bool Download(string filePath, string fileName, Uri destinationPath)////上面的代码实现了从ftp服务器下载文件的功能

{

try

{

String onlyFileName = Path.GetFileName(fileName);

string newFileName = filePath + "\\" + onlyFileName;

if (File.Exists(newFileName))

{

return false;

}

Uri uri = new Uri(destinationPath.AbsoluteUri+onlyFileName );

FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.DownloadFile);

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

Stream ftpStream = response.GetResponseStream();

long cl = response.ContentLength;

int bufferSize = 2048;

int readCount;

byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);

FileStream outputStream = new FileStream(newFileName, FileMode.Create);

while (readCount > 0)

{

outputStream.Write(buffer, 0, readCount);

readCount = ftpStream.Read(buffer, 0, bufferSize);

}

ftpStream.Close();

outputStream.Close();

response.Close();

return true;

}

catch (Exception ex)

{

return false;

}

}

}

}

调用方法

private FtpClientService ftpClient;

ftpClient = new FtpClientService();

string soucepath = Server.MapPath("zippath") + "\\99.zip";

//ftpClient.UploadFile(soucepath, new Uri("ftp://ip/a/"), 0, WebRequestMethods.Ftp.UploadFile);

string deltefile = "114.zip";

//ftpClient.DeleteFileName(deltefile ,new Uri("ftp://ip/a"));

string dirname = "992";

//ftpClient.MakeDir(dirname, new Uri("ftp://ip/a"));

//ftpClient.DeleteDir (dirname, new Uri("ftp://ip/a"));

// ftpClient.Rename(deltefile, "00.zip", new Uri("ftp://ip/a"));

ftpClient.Download("d:", "00.zip", new Uri("ftp://ip/a/"));

http://blog.sina.com.cn/s/blog_4c6e822d0102dsma.html

检查文件是否存在

private
bool FileRequest(string file)

{

FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(file);

ftp.Method = WebRequestMethods.Ftp.GetFileSize;

try

{

FtpWebResponse ftpresponse = (FtpWebResponse)ftp.GetResponse();

returntrue;

}

catch (WebException ex)

{

FtpWebResponse wr = (FtpWebResponse)ex.Response;

returnfalse;

}

catch

{

returnfalse;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐