.Net实现Web上传客户端文件到指定FTP

//FileStream, FileInfo等类只能处理本地服务器文件,无法读取客户端文件;读取文件流要使用Stream

public void UpLoadFileToFTP()

        {

            FtpWebRequest reqFTP;

            // 根据uri创建FtpWebRequest对象

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + strFtpFileName));

 

            // ftp用户名和密码

            reqFTP.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);

 

            // 默认为true,连接不会被关闭

            // 在一个命令之后被执行

            reqFTP.KeepAlive = false;

 

            // 指定执行什么命令

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

 

            // 指定数据传输类型

            reqFTP.UseBinary = true;

 

            //reqFTP.Timeout = 36000;

 

            // 上传文件时通知服务器文件的大小

            reqFTP.ContentLength = fuClientFileUpload.PostedFile.ContentLength;

 

            // 缓冲大小设置为10kb

            int buffLength = 10240;//2048

 

 

            byte[] buff = new byte[buffLength];

            int contentLen;

 

            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件

            //FileStream fs = fileInf.OpenRead();

 

            Stream fs = fuClientFileUpload.PostedFile.InputStream;

            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)

            {

                throw ex;

            }

        }

 

 

 

        /// <summary>

        /// 功能:删除Ftp文件 (在更新数据的同时将Ftp上的文件删除)

        ///撰写时间:20100427

        /// </summary>

        /// <param name="strFtpFileName"></param>

        public void DeleteFtpFile(string strFileName)

        {

            Uri uri = new Uri(strFtpUrl + strFileName);

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

 

            listRequest.Method = WebRequestMethods.Ftp.DeleteFile;

            listRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);

            FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse();

        }

 

 

        /// <summary>

        ///功能:下载ftp文档

        ///撰写时间:20100427

        /// </summary>

        /// <param name="strFtpFileName"></param>

        public void DownLoadFtpFile(string strFtpFileName)

        {

            string strFtpURL = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPURL"); //在線新聞上傳FTP地址

            string strFtpUserId = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPACCOUNT"); //FTP用户id

            string strFtpUserPwd = ParameterUtil.GetSysParameterValue("ONLINENEWSFTPPWD"); //FTP用户密码

 

            Uri uri = new Uri("ftp://10.127.0.1 ");

            WebClient request = new WebClient();

            request.Credentials = new NetworkCredential(strFtpUserId, strFtpUserPwd);

            byte[] newFileData = request.DownloadData(uri.ToString());

            FileStream fs = new FileStream(@"d:/abc.txt", FileMode.OpenOrCreate, FileAccess.Write);

            fs.Write(newFileData, 0, newFileData.Length);

            fs.Close();

        }

 

 

        /// <summary>

        /// 功能:检查文件是否存在

        ///撰写时间:20100427

        /// </summary>

        /// <param name="strFtpURL"></param>

        /// <param name="ftpUserName"></param>

        /// <param name="ftpPwd"></param>

        /// <returns></returns>

        public bool IsFileExist(string strFileName)

        {

            FtpWebRequest reqFTP;

            try

            {

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + strFileName));

                reqFTP.UseBinary = true;

                reqFTP.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);

                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;// WebRequestMethods.Ftp.ListDirectory;

                WebResponse response = reqFTP.GetResponse();

                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();

                if (line.Length > 0)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

            catch (Exception ex)

            {

                return false;

            }

        }

 

 

        /// <summary>

        /// 功能:重命名文件

        ///撰写时间:20100427

        /// </summary>

        /// <param name="currentFilename"></param>

        /// <param name="newFilename"></param>

        private bool fileRename(string currentFileName, string newFileName)

        {

            bool success = false;

            FtpWebRequest ftpWebRequest = null;

            FtpWebResponse ftpWebResponse = null;

            Stream ftpResponseStream = null;

 

            try

            {

                string uri = strFtpUrl + currentFileName;

 

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                ftpWebRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);

                ftpWebRequest.UseBinary = true;

                ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;

                ftpWebRequest.RenameTo = newFileName;

 

                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

                ftpResponseStream = ftpWebResponse.GetResponseStream();

 

                success = true;

            }

            catch (Exception)

            {

                success = false;

            }

            finally

            {

                if (ftpResponseStream != null)

                {

                    ftpResponseStream.Close();

                }

 

                if (ftpWebResponse != null)

                {

                    ftpWebResponse.Close();

                }

            }

            return success;

        }

 

        /// <summary>

        /// 文件存在检查

        /// </summary>

        /// <param name="ftpName"></param>

        /// <returns></returns>

        public bool fileCheckExist(string ftpName)

        {

            bool success = false;

            FtpWebRequest ftpWebRequest = null;

            WebResponse webResponse = null;

            StreamReader reader = null;

 

            try

            {

                string url = strFtpUrl;

 

                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

                ftpWebRequest.Credentials = new NetworkCredential(strFtpAccount, strFtpPassword);

                ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;

                ftpWebRequest.KeepAlive = false;

 

                webResponse = ftpWebRequest.GetResponse();

                reader = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);

                string line = reader.ReadLine();

                while (line != null)

                {

                    if (line == ftpName)

                    {

                        success = true;

                        break;

                    }

                    line = reader.ReadLine();

                }

            }

            catch (Exception)

            {

                success = false;

            }

            finally

            {

                if (reader != null)

                {

                    reader.Close();

                }

 

                if (webResponse != null)

                {

                    webResponse.Close();

                }

            }

            return success;

        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值