请问在winform里怎样才能连接到FTP,并获取文件路径。例如:ftp地址是:ftp://192.168.1.1/ 用户名是admin 密码是111 。登录FTP后,我选择了一个文件:picture.jpg 请问我怎样才能打开FTP目录,并且获得我鼠标选取的文件的FTP路径:ftp://192.168.1.1/picture.jpg ,感谢您的解答。
1条回答
- 花猫阳 2014-02-15 03:55关注
你是想通过FTP地址获取到FTP部署的本地路径,如把ftp://192.168.1.1/picture.jpg换成D:\FTP\picture.jpg,这个受FTP安全限制是无法获取的,FTP部署在服务器上面,就算获取到了实际路径你也没法操作,部署在本地这样获取就没什么意义了。
下面提供FTP的上传和下载的两个方法,供参考:
private bool UploadFileByFtpWebRequest(string fileName, string ftpServerIP, string path, string userName, string password)
{
bool result = true;
string url = string.Format("ftp://{0}:{1}/{2}", ftpServerIP, path, fileName.Substring(fileName.LastIndexOf(@"\") + 1));FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.UseBinary = true; request.Credentials = new NetworkCredential(userName, password); request.Method = WebRequestMethods.Ftp.UploadFile; Stream stream = request.GetRequestStream(); FileStream fileStream = new FileStream(fileName, FileMode.Open); int packageSize = 1024 * 1024; int packageCount = (int)(fileStream.Length / packageSize); int rest = (int)(fileStream.Length % packageSize); for (int index = 0; index < packageCount; index++) { byte[] buffer = new byte[packageSize]; fileStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, buffer.Length); this.pgbShow.Value = buffer.Length / (int)fileStream.Length * 100; //this.lblLeng.Text = pgbShow.Value.ToString(); lblLeng.Invoke(new myDelegate(delegate() { this.lblLeng.Text = pgbShow.Value.ToString() + "%"; })); } if (rest != 0) { byte[] buffer = new byte[rest]; fileStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, buffer.Length); this.BeginInvoke(new DoSomething(delegate() { //if (rest != 0) this.pgbShow.Maximum += 1; this.pgbShow.Value = 100; })); } stream.Close(); fileStream.Close(); this.BeginInvoke(new DoSomething(delegate() { //this.pgbShow.Value = 0; })); return result; } public string Download(string userId, string pwd, string ftpPath, string filePath, string fileName) { string sRet = "下载成功!"; FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create); // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName)); // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; // 指定数据传输类型 reqFTP.UseBinary = true; reqFTP.UsePassive = false; // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(userId, pwd); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); // 把下载的文件写入流 Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; // 缓冲大小设置为2kb int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; // 每次读文件流的2kb readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { // 把内容从文件流写入 outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } //关闭两个流和ftp连接 ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { sRet = ex.Message; } //返回下载结果(是否下载成功) return sRet; }
解决 无用评论 打赏 举报
悬赏问题
- ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?