public class FtpHelper
{
static string ftpServerIP = "ftp://127.0.0.1:2120";
static string ftpUserID = "admin";
static string ftpPassword = "password";
///
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;
}
}
原文:https://www.cnblogs.com/yzxcj/p/14745995.html