zoukankan      html  css  js  c++  java
  • c#Ftp上传文件

    public string UploadFile(string filePath, string fileName, string ftpServerIP, string ftpUserName, string ftpPassword)
    {
    string file = filePath + fileName;
    FileInfo fileInf = new FileInfo(file);

    string uri = "ftp://" + ftpServerIP + @"/" + fileInf.Name;
    FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
    reqFtp.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

    reqFtp.Method = WebRequestMethods.Ftp.UploadFile;

    reqFtp.UsePassive = false;
    reqFtp.UseBinary = true;
    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int contentLen = 0;
    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)
    {
    fs.Close();
    return ex.StackTrace;
    }
    return "文件上传成功";
    }

  • 相关阅读:
    Redis指令(2) ------String
    Redis指令(1) ------常用指令
    Redis数据类型
    Python set集合
    Python random 模块
    Python random 模块
    Python time 模块
    Python sys模块
    Python 递归函数
    Python 局部变量和全局变量
  • 原文地址:https://www.cnblogs.com/xwchengc/p/6343869.html
Copyright © 2011-2022 走看看