首页 新闻 赞助 找找看

请各位大虾 进来看看怎么用C#实现FTPS文件上传

0
悬赏园豆:100 [待解决问题]

公司要求ftp接口不能以明文方式传输,所以adc系统将增加ftps方式 但是在网找了很多方式都无法实现
用了
方法一

  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FileUploadPath);
  request.Method = WebRequestMethods.Ftp.ListDirectory;
  request.Credentials = new NetworkCredential(UpLoadUser,UpLoadPWD);

  request.EnableSsl = true;
  request.UsePassive = true;
  ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
  { return true; };

  FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  Stream responseStream = null;
  StreamReader readStream = null;
  try
  {
  responseStream = response.GetResponseStream();
  readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  if (readStream != null)
  Console.WriteLine(readStream.ReadToEnd());
  }
  finally
  {
  if (readStream != null)
  readStream.Close();
  if (response != null)
  response.Close();
  }  

方法二
  public void Upload(string filename)
  {
   
  FileInfo fileInf = new FileInfo(filename);
  string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
  FtpWebRequest reqFTP;

  // 根据uri创建FtpWebRequest对象 
  reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

  // ftp用户名和密码 
  reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  //SSL
  reqFTP.EnableSsl = true;
  // 默认为true,连接不会被关闭 
  // 在一个命令之后被执行 
  reqFTP.KeepAlive = false;
   
  // 指定执行什么命令 
  reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

  // 指定数据传输类型 
  reqFTP.UseBinary = true;
  //指定传输模式
  reqFTP.UsePassive =false;

  // 上传文件时通知服务器文件的大小 
  reqFTP.ContentLength = fileInf.Length;

  // 缓冲大小设置为2kb 
  int buffLength = 2048;

  byte[] buff = new byte[buffLength];
  int contentLen;

  // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 
  FileStream fs = fileInf.OpenRead();
  try
  {
  // 把上传的文件写入流 
  Stream strm = reqFTP.GetRequestStream();

  // 每次读文件流的2kb 
  contentLen = fs.Read(buff, 0, buffLength);

  // 流内容没有结束 
  while (contentLen != 0)
  {
  // 把内容从file stream 写入 upload stream 
  strm.Write(buff, 0, contentLen);

  contentLen = fs.Read(buff, 0, buffLength);
  }

  // 关闭两个流 
  strm.Close();
  fs.Close();
  }
  catch (Exception ex)
  {
  // MessageBox.Show(ex.Message, "Upload Error");
  }
  }

都是报远程链接失败 另外提一下 我们的服务器是不需要校验证书的 端口号是990 一定要是被动模式才能登录
麻烦各位大虾 帮忙看看有什么改进的办法 或者有直接的案例 提供一下

谢谢!

Di调*蝎的主页 Di调*蝎 | 初学一级 | 园豆:100
提问于:2010-08-31 13:19
< >
分享
所有回答(2)
0

看下这个

FTP C# Examples

http://www.example-code.com/csharp/ftp.asp

Secure FTP using C# and FTPS (FTP over SSL)

http://www.jscape.com/articles/ftp_using_csharp.html

[翻译].net 2.0(c#)下简单的FTP应用程序

http://www.cnblogs.com/webabcd/archive/2007/01/21/626242.html

 

HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-31 13:27
0

FTPS 不是以 "ftp://"开头的url吗?

连接失败的详细错误信息是什么?把Exception的StackTrace贴出来.

SSL连接话,服务器不验证证书,但是服务器必须得有证书.

Launcher | 园豆:45045 (高人七级) | 2010-08-31 13:32
你有没 关于 SSL加密的FTP 文件上传的例子呢
支持(0) 反对(0) Di调*蝎 | 园豆:100 (初学一级) | 2010-08-31 18:13
@Di调*蝎:我没有,但是可以做,不过,我首先会使用CuteFtp或telnet去测试部署的FTP是否部署正确,是ftp over ssl 还是 ftp over ssh.
支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2010-08-31 20:36
ftp over ssl
支持(0) 反对(0) Di调*蝎 | 园豆:100 (初学一级) | 2010-09-01 11:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册