第二次上传获取FtpWebRequest.GetRequestStream [英] Second Upload Get FtpWebRequest.GetRequestStream

查看:133
本文介绍了第二次上传获取FtpWebRequest.GetRequestStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

MY code:

//Clientpath = <<The full path where the file is located on client Machin 
        //Serverpath = <<The full serverpath where the file is creted on server.
        public void Upload(string Clientpath, string Serverpath)
        {

            try
            {
                FileInfo fileInf = new FileInfo(Clientpath);

                string uri = "ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name;

                FtpWebRequest reqFTP;

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.UsePassive = true;
                reqFTP.KeepAlive = true;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.Timeout = 3000;
             
                ServicePointManager.DefaultConnectionLimit = 10;

                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen;

                FileStream fs = fileInf.OpenRead();

                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();
                strm.Flush();
                strm.Dispose();
                strm = null;
                fs.Close();
                reqFTP.GetRequestStream().Close();
                reqFTP.Abort();
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
                
                reqFTP = null;


            }
            catch (Exception ex)
            {

                MessageBox.Show("Error:   " + ex);

            }
        }



请帮助我,我试着在过去3天内解决这个问题,我阅读了所有的博客和评论来自互联网,但仍然是床上运气,请检查此专家并解决我的问题



当我尝试第一次上传多个文件或第一次上传其工作良好。但是当第二个文件上传进程被挂起reqFTP.GetRequestStream();一段时间后超时


Please Help me i tried to resolve this issue from last 3 days,i read all blogs and comment from internet but still bed luck ,Please check this Experts and resolve my problem

When i try to upload multiple file at 1st time or 1st upload its working good.But when for second file upload process get hanged at "reqFTP.GetRequestStream();" and after some time is time out

推荐答案

老实说,这段代码中有很多噪音。

你正在调用GetRequestStream两次,从字面上看,你似乎只是在没有特定的顺序或原因调用每个流方法,我的意思是你关闭流然后刷新它然后处理它然后创建新流并立即关闭它然后中止它。 br />
我甚至不想猜测这里的第一个问题,但请注意,有很多理由说明为什么你没有从该代码中得到正确的结果。



不过尝试这样的事情:

Honestly there is a lot of noise in this snippet code.
You are calling GetRequestStream twice, also it literally seems like you are just calling every single stream method in no particular order or reason, I mean you are closing the stream and then flushing it and then disposing it and then creating new stream and instantly closing it and then aborting it.
I don't even want to guess what is the first issue here but do note that there are plenty of reasons why you not getting the right result from that code.

Nevertheless try something like this:
FileInfo fileInf = new FileInfo(Clientpath);
string uri = string.Format("ftp://{0}/{1}/{2}", ftpServerIP, Serverpath, fileInf.Name);

var reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.Timeout = 3000;

ServicePointManager.DefaultConnectionLimit = 10;

using (Stream strm = reqFTP.GetRequestStream())
using (FileStream fs = fileInf.OpenRead())
    fs.CopyTo(strm);


// Clientpath =<< The文件位于客户端上的完整路径Machin>>,

// Serverpath =<<服务器上创建文件的完整服务器路径>>,

public void Upload(string Clientpath,string Serverpath)

{

FileInfo fileInf = new FileInfo(Clientpath);



string uri =ftp://+ ftpServerIP +/+ Serverpath + fileInf.Name;



FtpWebRequest reqFTP;

reqFTP =(FtpWebRequest)FtpWebRequest.Create(new Uri(ftp://+ ftpServerIP +/+ Serverpath + fileInf.Name));

reqFTP.Credentials = new NetworkCredential (ftpUserID,ftpPassword);

reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;

int buffLength = 500000;

byte [] buff = new byte [buffLength];

int contentLen;

FileStream fs = fileInf.OpenRead();



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();

string result = String.Empty;

FtpWebResponse response =(FtpWebResponse)reqFTP.GetRespo nse();

流数据流= response.GetResponseStream();

StreamReader sr = new StreamReader(datastream);

result = sr。 ReadToEnd();

sr.Close();

datastream.Close();

response.Close();

reqFTP.Abort();



}
//Clientpath = <<The full path where the file is located on client Machin>>,
//Serverpath = <<The full serverpath where the file is creted on server>>,
public void Upload(string Clientpath, string Serverpath)
{
FileInfo fileInf = new FileInfo(Clientpath);

string uri = "ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name;

FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Serverpath + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 500000;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();

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();
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
reqFTP.Abort();

}


这篇关于第二次上传获取FtpWebRequest.GetRequestStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆