首页 新闻 会员 周边

.NET FTP上传文件 FtpWebRequest.GetRequestStream() 超时的问题。

0
悬赏园豆:30 [已解决问题] 解决于 2016-01-05 10:37
  public void Upload(string userId, string pwd, string filename, string ftpPath)
        {
            FileInfo fileInf = new FileInfo(filename);
            FtpWebRequest reqFTP;
            // 根据uri创建FtpWebRequest对象   
            reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileInf.Name));// 
            // ftp用户名和密码  
            reqFTP.Credentials = new NetworkCredential(userId, pwd);

            reqFTP.UsePassive = false;
            // 默认为true,连接不会被关闭  
            // 在一个命令之后被执行  
            reqFTP.KeepAlive = false;
            // 指定执行什么命令  
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // 指定数据传输类型  
            reqFTP.UseBinary = true;
            // 上传文件时通知服务器文件的大小  
            reqFTP.ContentLength = fileInf.Length;
            reqFTP.Timeout = 10*1000;
            // 缓冲大小设置为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)
            {

            }
        }

 

 

这是一个网上找来的方法。我上传到合作方的FTP服务器 他们给我的电脑开了白名单。

可以使用工具连接。

但是使用代码上传的时候。

reqFTP.GetRequestStream();  会超时,即时不加超时参数 也会超时。
userName、pwd 都正确。
在线等。。
众生皆苦的主页 众生皆苦 | 初学一级 | 园豆:138
提问于:2016-01-04 17:10
< >
分享
最佳答案
1

文件太大了?

收获园豆:30
CodeHsu | 大侠五级 |园豆:5468 | 2016-01-04 19:35

不是的  只是一张小图片。

众生皆苦 | 园豆:138 (初学一级) | 2016-01-04 19:36

@众生皆苦: 你改下这里:

1 reqFTP.UsePassive = true;
2 
3 reqFTP.KeepAlive = true;
CodeHsu | 园豆:5468 (大侠五级) | 2016-01-04 19:42

@XY.Seay: 可以了。您是正解。感谢。

众生皆苦 | 园豆:138 (初学一级) | 2016-01-05 10:23
其他回答(2)
0

把项目环境换成.net4.0试试

MrNice | 园豆:3450 (老鸟四级) | 2016-01-04 17:31

这项目版本不能改的。。

有没有替代的方法?

支持(0) 反对(0) 众生皆苦 | 园豆:138 (初学一级) | 2016-01-04 17:32

我写了个4.0的控制台 依然超时

支持(0) 反对(0) 众生皆苦 | 园豆:138 (初学一级) | 2016-01-04 17:42
0
        public void Upload(string filename)
        {
            IsDirectoryExist(ftpPath);
            FileInfo fileInf = new FileInfo(filename);
            string uri = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            FileStream fs = fileInf.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();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reqFTP != null)
                {
                    reqFTP.Abort();
                }
            }
        }

我是用的这个,和你不同的地方就一个,IsDirectoryExist,判断服务器是否存在要上传的目录.

水晶途途 | 园豆:1443 (小虾三级) | 2016-01-04 23:40

调用的是合作方的FTP服务器 我们的路径是经过协商肯定正确的(他们每天会代码创建第二天的路径)。

不过检查是否存在也是有必要的。

谢谢。

支持(0) 反对(0) 众生皆苦 | 园豆:138 (初学一级) | 2016-01-05 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册