如何利用FTP上传多个文件?


以下是我的FTP上传代码,这样循环对不对 
FtpWebRequest reqFTP;
            string uri = "ftp://xx.xx.xx.xx/";
            string[] fileList = Directory.GetFileSystemEntries("x:\\xxx");
            int buffLength = 2048;
            foreach (string file in fileList)
            {
                try
                {
                    FileInfo fileInf = new FileInfo(file);
                    //初始化FTP
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri + fileInf.Name));
                    reqFTP.Credentials = new NetworkCredential("xxx", "xxx");
                    reqFTP.KeepAlive = false;
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.UsePassive = false ;
                   
                    reqFTP.ContentLength = fileInf.Length;

                    // 缓冲大小设置为2kb

                    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();
                        //删除源文件
                        fileInf.Delete();

                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message, "Upload Error");
                        return;
                    }
                }
                catch (Exception exx)
                {
                    //MessageBox.Show(exx.Message, "Upload Error");
                    return;
                }


                // File.Delete(file);
            }

实际使用的时候容易返回421错误,是不是在对文件循环的时候重复初始化FTP引起的?

10 个解决方案

#1


没人知道啊?

#2


private static void UploadFile(string localFile)   
{   
FileInfo fi = new FileInfo(localFile);   
FileStream fs = fi.OpenRead();   
long length = fs.Length;   
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);   
req.Credentials = new NetworkCredential(FtpUid, FtpPwd);   
req.Method = WebRequestMethods.Ftp.UploadFile;   
req.UseBinary = true;   
req.ContentLength = length;   
req.Timeout = 10 * 1000;   
try   
{   
Stream stream = req.GetRequestStream();   
int BufferLength = 2048;   
byte[] b = new byte[BufferLength];   
int i;   
while ((i = fs.Read(b, 0, BufferLength)) > 0)   
{   
stream.Write(b, 0, i);   
}   
stream.Close();   
stream.Dispose();   

}   
catch (Exception ex)   
{   
}   

}   

#3


狠人来了吼吼,帮人帮到底,我是来抢分的


一、批量文件上传。

        //FtpURL:要上传到的FTP路径。

        Uri uriAddress = new Uri(FtpURL);

        //filename:上传前的文件名数组;filenames:上传后的文件名数组;UserName:FTP用户名; PassWord:FTP密码。

        FileCopys(filename, uriAddress, filenames, UserName, PassWord);

        #region 数据批量上传
        protected void FileCopys(string[] fileName, Uri uploadUrl, string[] fileNameS, string username, string password)
        {
            try
            {
                for (int i = 0; i < fileNameS.Length; i++)
                {
                    if (uploadUrl.Scheme == Uri.UriSchemeFtp)
                    {
                        FileInfo fileInf = new FileInfo(fileName[i]);
                        FtpWebRequest reqFTP;
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS[i]));
                        reqFTP.Credentials = new NetworkCredential(username, password);
                        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                        reqFTP.KeepAlive = false;
                        reqFTP.UseBinary = false;
                        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);
                        int allbye = (int)fileInf.Length;
                        int startbye = 0;
                        while (contentLen != 0)
                        {
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);
                            startbye += contentLen;
                        }
                        strm.Close();
                        fs.Close();
                    }
                }
            }
            catch
            {
                
            }
        }
        #endregion

二、单个文件上传

        //FtpURL:要上传到的FTP路径。

        Uri uriAddress = new Uri(FtpURL);

        //filename:需要上传的文件名;filename:上传后的文件名;UserName:FTP用户名; PassWord:FTP密码。

        FileCopy(filename, uriAddress, filename, UserName, PassWord);

        protected void FileCopy(string fileName, Uri uploadUrl, string fileNameS, string username, string password)
        {
            //FileCopy(InitFile1, uriAddress, FileNames1, UserName, PassWord);
            //FileCopy(InitFile5, uriAddress, FileNames5, UserName, PassWord);
            //FileCopy(InitFile6, uriAddress, FileNames6, UserName, PassWord);
            //FileCopy(InitFile7, uriAddress, FileNames7, UserName, PassWord);
            //FileCopy(InitFile8, uriAddress, FileNames8, UserName, PassWord);
            //FileCopy(InitFile9, uriAddress, FileNames9, UserName, PassWord);
            //FileCopy(InitFile11, uriAddress, FileNames11, UserName, PassWord);
            try
            {
                if (uploadUrl.Scheme == Uri.UriSchemeFtp)
                {
                    FileInfo fileInf = new FileInfo(fileName);
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS));
                    reqFTP.Credentials = new NetworkCredential(username, password);
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.KeepAlive = false;
                    reqFTP.UseBinary = false;
                    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);
                    int allbye = (int)fileInf.Length;
                    int startbye = 0;
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                        startbye += contentLen;
                    }
                    strm.Close();
                    fs.Close();
                }
            }
            catch
            {
                //MessageBox.Show("FTP站点用户名或密码错误,请与管理员联系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

#4


晕,如梦怎么这么快啊~~~~~~~~~~~~~~郁闷

#5


楼主直接把我的方法拷贝下来,在你的程序引用方法即可。多文件单文件上传只要调用方法就行了,很方便

#6


 Stream strm = reqFTP.GetRequestStream();
上面这一句很容易产生“连接超时”的异常,不知道为什么

#7


你的测试网络环境是怎样的呢?

#8


网络正常啊,我用FTP工具上传都没有问题的

#9


@yalan,您多文件上传的代码,我放在程序中不行,一个都上传上去,报550错误。

#10


引用 9 楼 gyc1a2b3c 的回复:
@yalan,您多文件上传的代码,我放在程序中不行,一个都上传上去,报550错误。

报这个错误是因为你们的方法中都多次new了这个 reqFTP.Credentials = new NetworkCredential(username, password); 在同一个帐户连接时多次new就会报550错误,单文件上传也不例外,需要优化,我以前做这个的时候就被这个折腾了下,给你个解决方案:  ftpWebRqt = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServer));
                if (isnewNetWork == false || networkCredential.UserName != username || networkCredential.Password != password)
                {
                    networkCredential = new NetworkCredential(username, password);
                    isnewNetWork = true;
                }
                ftpWebRqt.Credentials = networkCredential;
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告