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

来源:互联网 发布:js返回上一个页面 编辑:程序博客网 时间:2024/05/17 23:04

文章转自:   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.} 

原创粉丝点击