zoukankan      html  css  js  c++  java
  • ftp上传

    public class FtpUtil
        {
            string ftpServerIP;
            string ftpRemotePath;
            string ftpUserID;
            string ftpPassword;
            string ftpURI;
    
            /// <summary>
            /// 连接FTP
            /// </summary>
            /// <param name="FtpServerIP">FTP连接地址</param>
            /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
            /// <param name="FtpUserID">用户名</param>
            /// <param name="FtpPassword">密码</param>
            public FtpUtil(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
            {
                ftpServerIP = FtpServerIP;
                ftpRemotePath = FtpRemotePath;
                ftpUserID = FtpUserID;
                ftpPassword = FtpPassword;
                ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
            }
    
            /// <summary>
            /// 上传
            /// </summary>
            /// <param name="filename"></param>
            public void Upload(string filename)
            {
                FileInfo fileInf = new FileInfo(filename);
                string uri = ftpURI + fileInf.Name;
                FtpWebRequest reqFTP;
    
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = false;
                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;
                FileStream fs = fileInf.OpenRead();
                try
                {
                    Stream strm = reqFTP.GetRequestStream();
                    contentLen = fs.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    throw new Exception("Ftphelper Upload Error --> " + ex.Message);
                }
            }
    
        }
  • 相关阅读:
    SQL 生成可配置流水号
    安卓程序进入后台和前台的判断
    Android代码故事第一回,平均间隔的按钮
    安卓冷知识:LayoutParams
    初识Android NDK
    搬家
    LaTeX表格紧跟文字 (不影响下方文本对齐)
    FlagCounter被封杀?自己实现一个简单的多国访客计数器
    Python+OpenCV竖版古籍文字分割
    Ubuntu18.04 显卡驱动+Cuda安装踩坑记录 以及Ubuntu虚拟内存的添加
  • 原文地址:https://www.cnblogs.com/tangchun/p/9967525.html
Copyright © 2011-2022 走看看