FTP文件操作 上传文、 下载文件、删除文件 、创建目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/// <summary>
        /// FTP上传文件
        /// </summary>
        /// <param name="fileUpload">上传控件</param>
        /// <param name="ftpServerIP">上传文件服务器IP</param>
        /// <param name="ftpUserID">服务器用户名</param>
        /// <param name="ftpPassword">服务器密码</param>
        /// <returns></returns>
        public  string Upload(FileUpload fileUpload, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            string filename = fileUpload.FileName;
            string sRet = "上传成功!";
            FileInfo fileInf = new FileInfo(fileUpload.PostedFile.FileName);
            string uri = "ftp://" + ftpServerIP + "/" + filename;
            FtpWebRequest reqFTP;
 
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
 
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
 
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
            reqFTP.KeepAlive = false;
 
            // 指定执行什么命令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
 
            // 指定数据传输类型
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
 
            // 上传文件时通知服务器文件的大小
            reqFTP.ContentLength = fileInf.Length;
 
            // 缓冲大小设置为2kb
            int buffLength = 2048;
 
            byte[] buff = new byte[buffLength];
            int contentLen;
 
            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
            FileStream fs = fileInf.OpenRead();
 
            try
            {
                // 把上传的文件写入流
                Stream strm = reqFTP.GetRequestStream();
 
                // 每次读文件流的2kb
                contentLen = fs.Read(buff, 0, buffLength);
 
                // 流内容没有结束
                while (contentLen != 0)
                {
                    // 把内容从file stream 写入 upload stream
                    strm.Write(buff, 0, contentLen);
 
                    contentLen = fs.Read(buff, 0, buffLength);
                }
 
                // 关闭两个流
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            return sRet;
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// <summary>
        /// FTP下载文件
        /// </summary>
        /// <param name="userId">ftp用户名</param>
        /// <param name="pwd">ftp密码</param>
        /// <param name="ftpPath">ftp文件路径</param>
        /// <param name="filePath">下载保存路径</param>
        /// <param name="fileName">ftp文件名</param>
        /// <returns></returns>
        public string  Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
        {
            string sRet = "下载成功!"
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
 
                // 根据uri创建FtpWebRequest对象  
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
 
                // 指定执行什么命令 
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
 
                // 指定数据传输类型 
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
 
                // ftp用户名和密码 
                reqFTP.Credentials = new NetworkCredential(userId, pwd);
                 
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 
                // 把下载的文件写入流
                Stream ftpStream = response.GetResponseStream();
 
                long cl = response.ContentLength;
 
                // 缓冲大小设置为2kb 
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
 
                // 每次读文件流的2kb 
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    // 把内容从文件流写入  
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
 
                //关闭两个流和ftp连接
                ftpStream.Close();
                outputStream.Close();
                response.Close();
 
 
            }
            catch (Exception ex)
            {
                sRet=ex.Message;
            }
 
            //返回下载结果(是否下载成功)
            return sRet;
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// <summary>
        /// FTP删除文件
        /// </summary>
        /// <param name="ftpPath">ftp文件路径</param>
        /// <param name="userId">ftp用户名</param>
        /// <param name="pwd">ftp密码</param>
        /// <param name="fileName">ftp文件名</param>
        /// <returns></returns>
        public string DeleteFile(string ftpPath,string userId,string pwd, string fileName)
        {
            string sRet = "删除成功!";  
            FtpWebResponse Respose = null;
            FtpWebRequest reqFTP = null;
            Stream localfile = null;
            Stream stream = null;
            try
            {
                //根据uri创建FtpWebRequest对象 
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(string.Format(@"{0}{1}", ftpPath, fileName)));
 
                //提供账号密码的验证 
                reqFTP.Credentials = new NetworkCredential(userId, pwd);   
 
                //默认为true是上传完后不会关闭FTP连接 
                reqFTP.KeepAlive = false;   
 
                //执行删除操作
                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
 
                Respose = (FtpWebResponse)reqFTP.GetResponse();     
            }          
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
 
            finally
            {
                //关闭连接跟流
                if (Respose != null)
                    Respose.Close();
                if (localfile != null)
                    localfile.Close();
                if (stream != null)
                    stream.Close();
            }
 
            //返回执行状态
            return sRet;
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// <summary>
        /// FTP创建目录
        /// </summary>
        /// <param name="dirName">目录名</param>
        /// <param name="ftpServerIP">服务器地址</param>
        /// <param name="ftpUserID">ftp用户名</param>
        /// <param name="ftpPassword">ftp密码</param>
        /// <returns></returns>
        public  string CreateDir(string dirName, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            string sRet = "OK";
            try
            {
                string uri =  ftpServerIP + "/" + dirName;
                FtpWebRequest reqFTP;
 
                // 根据uri创建FtpWebRequest对象  
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
 
                // ftp用户名和密码 
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
 
                // 默认为true,连接不会被关闭 
                // 在一个命令之后被执行 
                reqFTP.KeepAlive = false;
 
                // 指定执行什么命令 
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
 
                // 指定数据传输类型 
                reqFTP.UseBinary = true;
 
                FtpWebResponse respFTP = (FtpWebResponse)reqFTP.GetResponse();
                respFTP.Close();
            }
            catch (Exception ex)
            {
                sRet = ex.Message;
            }
            return sRet;
        }

  

posted @   码先行  阅读(1301)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
· C# 13 中的新增功能实操
点击右上角即可分享
微信分享提示