/// 上传文件到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 = CommConst.ftpName
string uri = string.Empty
if (!string.IsNullOrEmpty(strPath))
{
FileInfo file = new FileInfo(strPath)
if (!file.Exists)
{
res = false
}
else
{
uri = ftpUrl + ftpServerIp + file.Name
FtpWebRequest reqFtp=(FtpWebRequest)WebRequest.Create(new Uri(uri))
DealFile(ftpServerIp, userName, userPass)//查看服务器上是否存在伍顷指定文件夹
reqFtp.Credentials = new NetworkCredential(userName, userPass)
reqFtp.KeepAlive = false
reqFtp.Method = WebRequestMethods.Ftp.UploadFile
reqFtp.UseBinary = true
reqFtp.ContentLength = file.Length
int buffLength = 2048
byte[] buff = new byte[buffLength]
int contentLen
FileStream fs = file.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()
res = true
}
catch (Exception ex)
{
Console.Write(ex.Message)
return false
}
}
}
return res
}
/// <summary>
/// 处理上传文件的路径,如果不存在世橘派,新建
/// </summary>
public static void DealFile(string ftpServerIp, string userName, string userPass)
{
string[] Paths = ftpServerIp.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
string realftpURL = CommConst.ftpName + Paths[0]//需要查询的ftp路径
if (Paths!=null && Paths.Length >= 2)
{
string fileName = ""
for (int i = 0i <Paths.Length - 1i++)
{
if (i != 0)
{
fileName = Paths[i] + CommConst.bevelSign + Paths[i + 1]
}
else
{
fileName = Paths[i + 1]
}
if (!fileIsexit(realftpURL, userName, userPass, fileName))//如搜贺果文件夹不存在
{
// realftpURL += CommConst.bevelSign + Paths[i + 1]
CreateFile(realftpURL, userName, userPass,Paths[i+1])
}
realftpURL += CommConst.bevelSign + Paths[i + 1]
}
}
}
这个是打印侍态log日志,你这个老渣源是答应uri,而且你的=写在了“”外面,TAG是个String是你标签,随便起,你用的是e,梁团打印的是红色的,为error级别,不过无所谓,我习惯开发的时候用.e,红色看的明显欢迎分享,转载请注明来源:内存溢出
评论列表(0条)