FTP上传文件 未能确定的uri 的格式怎么解决

 我来答
EvenHHZ
2017-02-05 · 知道合伙人软件行家
EvenHHZ
知道合伙人软件行家
采纳数:13687 获赞数:18847
个人出版图书:《玩转Python网络爬虫》、《玩转Django2.0》

向TA提问 私信TA
展开全部
/// <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 = 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 = 0; i < Paths.Length - 1; i++)
{
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];
}
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式