C#winform ftp上传下载

public class FtpHelper
{
static string ftpServerIP = "ftp://127.0.0.1:2120";
static string ftpUserID = "admin";
static string ftpPassword = "password";
///


/// FTP下载
///

public static void FtpDownload(string baobiaoid)
{
string str = "/Report/" + baobiaoid + ".xml";
string s = "Report/" + baobiaoid + ".xml";
string filename = baobiaoid + ".xml";
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/Report/"));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
string line = reader.ReadToEnd();
var index = line.IndexOf(filename);
if (index == -1)
{
reqFTP = null;
response.Close();
reader.Close();

            FileStream fs1 = new FileStream(s, FileMode.Create, FileAccess.Write);//创建写入文件 
            StreamWriter sw = new StreamWriter(fs1);
            sw.Close();
            fs1.Close();

            XmlDocument doc = new XmlDocument();
            string st = "Report/mb.xml";
            doc.Load(@st);
            string a = doc.OuterXml.ToString();
            string[] ster = a.Split();
            File.AppendAllLines(@s, ster);
        }
        else
        {
            reqFTP = null;
            response.Close();
            reader.Close();
            FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpServerIP + str);
            ftpWebRequest.Credentials = new System.Net.NetworkCredential(ftpUserID, ftpPassword);
            ftpWebRequest.UseBinary = true;
            ftpWebRequest.UsePassive = true;
            ftpWebRequest.KeepAlive = true;

            ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
            Stream ftpStream = ftpWebResponse.GetResponseStream();
            FileStream localFileStream = new FileStream(s, FileMode.Create);
            int bufferSize = 1 * 1024 * 1024;
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            localFileStream.Close();
            ftpStream.Close();
            ftpWebResponse.Close();
            ftpWebRequest = null;
        }
    }
    /// <summary>
    /// FTP上传
    /// </summary>
    /// <param name="strFtpPath"></param>
    /// <param name="strFileName"></param>
    /// <param name="strFtpUser"></param>
    /// <param name="strFtpPassWord"></param>
    /// <param name="strUploadFileName"></param>
    public static void FtpUpload(string baobiaoid)
    {
        string str = "/Report/" + baobiaoid + ".xml";
        string s = "Report/" + baobiaoid + ".xml";
        FileInfo uploadFileInfo = new FileInfo(s);//待上传文件
        FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpServerIP + str);//上传的地址名称

        ftpWebRequest.UseBinary = true;
        ftpWebRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        ftpWebRequest.KeepAlive = false;
        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpWebRequest.ContentLength = uploadFileInfo.Length;

        Stream ftpStream = ftpWebRequest.GetRequestStream();

        FileStream uploadFileReadStream = uploadFileInfo.OpenRead();

        int bufferSize = 1 * 1024 * 1024;

        byte[] byteBuffer = new byte[bufferSize];
        int bytesRead = uploadFileReadStream.Read(byteBuffer, 0, bufferSize);
        try
        {
            while (bytesRead > 0)
            {
                ftpStream.Write(byteBuffer, 0, bytesRead);
                bytesRead = uploadFileReadStream.Read(byteBuffer, 0, bufferSize);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        ftpStream.Close();
        uploadFileReadStream.Close();
        ftpWebRequest = null;
    }
}

C#winform ftp上传下载

上一篇:API文档使用方法


下一篇:Windows服务器与本地电脑无法远程复制粘贴怎么办?