ASP.NET 使用FTP文件上传、下载文件

文章转自:   http://blog.csdn.net/ZZJ_4Ever/article/details/4038575  

 

01./// <summary> 
02.///
05.{ 
06.    string ftpServerIP; 
07.    string ftpRemotePath;  FtpWeb 的摘要说明
03./// </summary>
04.public class FtpWeb 
08.    string ftpUserID; 
09.    string ftpPassword; 
10.    string ftpURI; 
11. 
12.    /// <summary> 
13.    /// 连接FTP 
14.    /// </summary> 
15.    /// <param name="FtpServerIP">FTP连接地址</param> 
16.    /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param> 
17.    /// <param name="FtpUserID">用户名</param> 
18.    /// <param name="FtpPassword">密码</param> 
19.    public FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword) 
20.    { 
21.        ftpServerIP = FtpServerIP; 
22.        ftpRemotePath = FtpRemotePath; 
23.        ftpUserID = FtpUserID; 
24.        ftpPassword = FtpPassword; 
25.        ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/"; 
26.    } 
27. 
28.    /// <summary> 
29.    /// 上传 
30.    /// </summary> 
31.    /// <param name="filename"></param> 
32.    public bool Upload(string filename, out string errorMsg) 
33.    { 
34.        errorMsg = ""; 
35.        FileInfo fileInf = new FileInfo(filename); 
36.        string uri = ftpURI + fileInf.Name; 
37.        FtpWebRequest reqFTP; 
38. 
39.        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 
40.        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
41.        reqFTP.KeepAlive = false; 
42.        reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
43.        reqFTP.UseBinary = true; 
44.        reqFTP.ContentLength = fileInf.Length; 
45.        int buffLength = 2048; //开辟2KB缓存区 
46.        byte[] buff = new byte[buffLength]; 
47.        int contentLen; 
48.        FileStream fs = fileInf.OpenRead(); 
49.        try 
50.        { 
51.            Stream strm = reqFTP.GetRequestStream(); 
52.            contentLen = fs.Read(buff, 0, buffLength); 
53.            while (contentLen != 0) 
54.            { 
55.                strm.Write(buff, 0, contentLen); 
56.                contentLen = fs.Read(buff, 0, buffLength); 
57.            } 
58.            strm.Close(); 
59.            fs.Close(); 
60.            return true; 
61.        } 
62.        catch (Exception ex) 
63.        { 
64.            errorMsg = ex.Message; 
65.            return false; 
66.        } 
67.    } 
68. 
69.    /// <summary> 
70.    /// 下载 
71.    /// </summary> 
72.    /// <param name="filePath"></param> 
73.    /// <param name="fileName"></param> 
74.    public bool Download(string filePath, string fileName, out string errorMsg) 
75.    { 
76.        errorMsg = ""; 
77.        FtpWebRequest reqFTP; 
78.        try 
79.        { 
80.            FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create); 
81. 
82.            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName)); 
83.            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
84.            reqFTP.UseBinary = true; 
85.            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
86.            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
87.            Stream ftpStream = response.GetResponseStream(); 
88.            long cl = response.ContentLength; 
89.            int bufferSize = 2048; 
90.            int readCount; 
91.            byte[] buffer = new byte[bufferSize]; 
92. 
93.            readCount = ftpStream.Read(buffer, 0, bufferSize); 
94.            while (readCount > 0) 
95.            { 
96.                outputStream.Write(buffer, 0, readCount); 
97.                readCount = ftpStream.Read(buffer, 0, bufferSize); 
98.            } 
99. 
100.            ftpStream.Close(); 
101.            outputStream.Close(); 
102.            response.Close(); 
103.            return true; 
104.        } 
105.        catch (Exception ex) 
106.        { 
107.            errorMsg = ex.Message; 
108.            return false; 
109.        } 
110.    }  
111.} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值