c# ftp上传方式把文件上传到指定服务器!

c# ftp上传方式把文件上传到指定服务器!,第1张

/// <summary>
/// 上传文件到FTP服务器
/// </summary>
/// <param name="ftpServerIp">FTP访问地址</param>
/// <param name="userName">用户名</param>
/// <param name="userPass">密码</param>
/// <param name="strPath">地址</param>
/// <returns></returns>
public static bool UpLoadFileFtp(string ftpServerIp, string userName, string userPass, string filePath, string strPath)
{
bool res =false;
string ftpUrl = CommConstftpName;
string uri = stringEmpty;
if (!stringIsNullOrEmpty(strPath))
{
FileInfo file = new FileInfo(strPath);
if (!fileExists)
{
res = false;
}
else
{
uri = ftpUrl + ftpServerIp + fileName;
FtpWebRequest reqFtp=(FtpWebRequest)WebRequestCreate(new Uri(uri));
DealFile(ftpServerIp, userName, userPass);//查看服务器上是否存在指定文件夹
reqFtpCredentials = new NetworkCredential(userName, userPass);
reqFtpKeepAlive = false;
reqFtpMethod = WebRequestMethodsFtpUploadFile;
reqFtpUseBinary = true;
reqFtpContentLength = fileLength;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileOpenRead();
try
{
Stream strm = reqFtpGetRequestStream();
contentLen = fsRead(buff, 0, buffLength);
while (contentLen != 0)
{
strmWrite(buff, 0, contentLen);
contentLen = fsRead(buff, 0, buffLength);
}

strmClose();
fsClose();
res = true;
}
catch (Exception ex)
{
ConsoleWrite(exMessage);
return false;
}
}
}
return res;
}
/// <summary>
/// 处理上传文件的路径,如果不存在,新建
/// </summary>
public static void DealFile(string ftpServerIp, string userName, string userPass)
{
string[] Paths = ftpServerIpSplit(new char[] { '/' }, StringSplitOptionsRemoveEmptyEntries);
string realftpURL = CommConstftpName + Paths[0];//需要查询的ftp路径
if (Paths!=null && PathsLength >= 2)
{
string fileName = "";
for (int i = 0; i < PathsLength - 1; i++)
{
if (i != 0)
{
fileName = Paths[i] + CommConstbevelSign + Paths[i + 1];
}
else
{
fileName = Paths[i + 1];
}

if (!fileIsexit(realftpURL, userName, userPass, fileName))//如果文件夹不存在
{
// realftpURL += CommConstbevelSign + Paths[i + 1];
CreateFile(realftpURL, userName, userPass,Paths[i+1]);
}
realftpURL += CommConstbevelSign + Paths[i + 1];
}
}

}

你的linux上已经开启了FTP服务的话,那么执行如下命令: ftp linux服务器IP地址。 put 文件名。 如果你需要上传很多的文件的话,那么建议你使用FTP工具,比如: flashfxp cuteftp leapftp 等工具都可以实现的。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/12844469.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存