c# 连接ftp服务器,获取文件成功,下载失败550,求助

夺命_书生 2019-04-08 06:12:43
c#连接ftp服务器,连接获取文件夹的文件列表成功,下载单个文件和获取文件大小返回都返回550,路径正确,主动被动都失败,用FTP工具则下载正常,求助
...全文
960 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
生财 2020-12-01
  • 打赏
  • 举报
回复
楼主可以考虑换个FTP的软件试试.FTP下载文件代码类似并且很简单.
xiechengxinhemi 2020-11-30
  • 打赏
  • 举报
回复
你好 朋友 请问解决了吗 download 下载失败的问题
夺命_书生 2019-04-11
  • 打赏
  • 举报
回复
引用 8 楼 夺命_书生 的回复:
引用 6 楼 stherix 的回复:
[quote=引用 5 楼 夺命_书生 的回复:] [quote=引用 3 楼 stherix 的回复:][quote=引用 2 楼 夺命_书生 的回复:] [quote=引用 1 楼 stherix 的回复:]多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
尝试用在地址里加上你的用户名密码看下 比如 ftp://用户名:密码@地址:端口/路径[/quote]请问ftp执行下载或获取文件流是同一个命令吗[/quote] 下载文件和获取文件流其实是一个东西吧 最终是需要GET命令来实际下载[/quote]求教[/quote]七楼内容
夺命_书生 2019-04-11
  • 打赏
  • 举报
回复
引用 6 楼 stherix 的回复:
引用 5 楼 夺命_书生 的回复:
[quote=引用 3 楼 stherix 的回复:][quote=引用 2 楼 夺命_书生 的回复:] [quote=引用 1 楼 stherix 的回复:]多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
尝试用在地址里加上你的用户名密码看下 比如 ftp://用户名:密码@地址:端口/路径[/quote]请问ftp执行下载或获取文件流是同一个命令吗[/quote] 下载文件和获取文件流其实是一个东西吧 最终是需要GET命令来实际下载[/quote]求教
夺命_书生 2019-04-11
  • 打赏
  • 举报
回复
请问用c# FtpWebRequest类只能获取到文件列表,下载就550了,用cmd去get又正常,这是什么原因
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Configuration; namespace ScheduleTask { public class FtpTool { private static string ftpUri = ConfigurationManager.AppSettings["FtpUri"]; private static string folderPath = ConfigurationManager.AppSettings["ScheduleTaskFolder"]; private static string ftpFullDirectoryPath = Path.Combine(ftpUri, folderPath);//Ftp存储文件所在文件夹入径 private static string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"]; private static string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"]; public static bool Upload(string fileFullPath) { bool isSuccess = false; FileInfo fileinfo = new FileInfo(fileFullPath); string uri = Path.Combine(ftpFullDirectoryPath, fileinfo.Name);//Ftp存储文件全入径 if (!MakeDir(ftpFullDirectoryPath)) return isSuccess;//若文件夹不存在则创建 try { var tempPath = Download(uri, fileinfo.Name); if (tempPath != null) { System.Diagnostics.FileVersionInfo fileVersion1 = System.Diagnostics.FileVersionInfo.GetVersionInfo(fileFullPath); System.Diagnostics.FileVersionInfo fileVersion2 = System.Diagnostics.FileVersionInfo.GetVersionInfo(tempPath); Version v1 = new Version(fileVersion1.FileVersion != null ? fileVersion1.FileVersion : "0.0"); Version v2 = new Version(fileVersion2.FileVersion != null ? fileVersion1.FileVersion : "0.0"); if (v1 < v2) return isSuccess = true; DeleteFile(uri);//若ftp存在版本低于当前上传文件版本,则删除ftp文件 } isSuccess = true; } catch (Exception ex) { throw ex; } if (!isSuccess) return false; else isSuccess = false; FtpWebRequest ftpReq; ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftpReq.KeepAlive = false; ftpReq.Proxy = null; ftpReq.Method = WebRequestMethods.Ftp.UploadFile; ftpReq.UseBinary = true; ftpReq.ContentLength = fileinfo.Length; int bufferLength = 2048; byte[] buffer = new byte[bufferLength]; int contentLen = 0; FileStream fs = fileinfo.OpenRead(); Stream stream = null; try { stream = ftpReq.GetRequestStream(); contentLen = fs.Read(buffer, 0, bufferLength); while (contentLen != 0) { stream.Write(buffer, 0, bufferLength); contentLen = fs.Read(buffer, 0, bufferLength); } stream.Close(); fs.Close(); fs.Close(); isSuccess = true; } catch (Exception ex) { if (stream != null) stream.Close(); if (fs != null) fs.Close(); throw ex; } return isSuccess; } public static string Download(string fileFullPath, string fileName) { var basePath = System.AppDomain.CurrentDomain.BaseDirectory; var tempDirectory = basePath.Substring(0,basePath.LastIndexOf(@"\")+1)+ "TempDirectory\\"; if (!Directory.Exists(tempDirectory)) Directory.CreateDirectory(tempDirectory); var tempFileFullPath = tempDirectory + fileName; if (File.Exists(tempFileFullPath)) File.Delete(tempFileFullPath); Stream stream = null; FileStream outPutStream = new FileStream(tempFileFullPath, FileMode.Create); FtpWebRequest ftpReq = null; FtpWebResponse ftpres = null; ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileFullPath)); ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftpReq.KeepAlive = false; ftpReq.Proxy = null; ftpReq.Method = WebRequestMethods.Ftp.DownloadFile; try { ftpres = (FtpWebResponse)ftpReq.GetResponse(); stream = ftpres.GetResponseStream(); long cl = ftpres.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = stream.Read(buffer, 0, bufferSize); while (readCount > 0) { outPutStream.Write(buffer, 0, bufferSize); readCount = stream.Read(buffer, 0, bufferSize); } stream.Close(); outPutStream.Close(); ftpres.Close(); } catch (Exception ex) { tempFileFullPath = null; if (stream != null) stream.Close(); if (outPutStream != null) outPutStream.Close(); if (ftpres != null) ftpres.Close(); } return tempFileFullPath; } //判断目录在ftp中是否存在 public static bool RemoteFtpDirExists(string path) { FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); reqFtp.UseBinary = true; reqFtp.KeepAlive = false; reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFtp.Method = WebRequestMethods.Ftp.ListDirectory; FtpWebResponse resFtp = null; try { resFtp = (FtpWebResponse)reqFtp.GetResponse(); FtpStatusCode code = resFtp.StatusCode;//OpeningData resFtp.Close(); return true; } catch (Exception ex) { if (resFtp != null) { resFtp.Close(); } return false; } } //在ftp中创建目录 public static bool MakeDir(string dirName) { try { bool b = RemoteFtpDirExists(dirName); if (b) { return true; } FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(dirName)); reqFtp.UseBinary = true; reqFtp.KeepAlive = false; reqFtp.Method = WebRequestMethods.Ftp.MakeDirectory; reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse(); response.Close(); return true; } catch (Exception ex) { return false; } } public string[] GetFileList(string url, string method) { url = ftpUri + url; string[] downloadFiles; StringBuilder result = new StringBuilder(); try { FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); reqFtp.UseBinary = true; reqFtp.KeepAlive = false; reqFtp.Method = method; reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); WebResponse response = reqFtp.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名 string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append(" "); line = reader.ReadLine(); } if (result.Length == 0) return null; result.Remove(result.ToString().LastIndexOf(' '), 1); reader.Close(); response.Close(); return result.ToString().Split(' '); } catch (Exception ex) { downloadFiles = null; return downloadFiles; } } public static void DeleteFile(string fileName) { FtpWebRequest ftpreq = (FtpWebRequest)WebRequest.Create(fileName); ftpreq.UseBinary = true; ftpreq.Method = WebRequestMethods.Ftp.DeleteFile; ftpreq.KeepAlive = false; ftpreq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse resFtp = null; try { resFtp = (FtpWebResponse)ftpreq.GetResponse(); resFtp.Close(); } catch (Exception ex) { if (resFtp != null) { resFtp.Close(); } throw ex; } } public static bool DeleteDirectory(string directoryName) { var uri = ftpUri + directoryName; FtpWebRequest ftpreq = (FtpWebRequest)WebRequest.Create(uri); ftpreq.UseBinary = true; ftpreq.Method = WebRequestMethods.Ftp.DeleteFile; ftpreq.KeepAlive = false; ftpreq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse resFtp = null; try { resFtp = (FtpWebResponse)ftpreq.GetResponse(); resFtp.Close(); return true; } catch (Exception) { if (resFtp != null) { resFtp.Close(); } return fals
  • 打赏
  • 举报
回复
我有这个类,挺好用的
stherix 2019-04-09
  • 打赏
  • 举报
回复
引用 2 楼 夺命_书生 的回复:
引用 1 楼 stherix 的回复:
多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
尝试用在地址里加上你的用户名密码看下 比如 ftp://用户名:密码@地址:端口/路径
stherix 2019-04-09
  • 打赏
  • 举报
回复
引用 5 楼 夺命_书生 的回复:
引用 3 楼 stherix 的回复:
[quote=引用 2 楼 夺命_书生 的回复:] [quote=引用 1 楼 stherix 的回复:]多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
尝试用在地址里加上你的用户名密码看下 比如 ftp://用户名:密码@地址:端口/路径[/quote]请问ftp执行下载或获取文件流是同一个命令吗[/quote] 下载文件和获取文件流其实是一个东西吧 最终是需要GET命令来实际下载
夺命_书生 2019-04-09
  • 打赏
  • 举报
回复
引用 3 楼 stherix 的回复:
引用 2 楼 夺命_书生 的回复:
[quote=引用 1 楼 stherix 的回复:]多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
尝试用在地址里加上你的用户名密码看下 比如 ftp://用户名:密码@地址:端口/路径[/quote]请问ftp执行下载或获取文件流是同一个命令吗
夺命_书生 2019-04-09
  • 打赏
  • 举报
回复
用地址打不开,用了ftpclient类,发现执行命令RETR 路径也是550,但是OpenRead读文件就可以读取文件,不解!
夺命_书生 2019-04-09
  • 打赏
  • 举报
回复
引用 1 楼 stherix 的回复:
多半还是路径问题,检查下大小写,斜杠,空格之类的
配的路径可以获取文件列表,但是路径+文件名用来获取文件大小和下载都出错了
stherix 2019-04-08
  • 打赏
  • 举报
回复
多半还是路径问题,检查下大小写,斜杠,空格之类的

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧