我需要通过FTP将文件从一台服务器传输到另一台服务器。我已经使用了下面的代码。
在很多文件中,这只是部分传输单个文件。例如。我有一个56KB的源文件。在运行下面的代码后,源文件将减少到0kb,并将0KB文件传输到目标文件,而不是56 KB的文件大小。
我构建了将所有文件从源文件传输到目标文件的代码。但是,如上所述,在传输单个0KB文件后,它并没有取得进一步的进展。
请帮助我。
static void Main(string [] args) { 字符串DISCH_DEST = System.Configuration.ConfigurationManager.AppSettings [DISCH_DEST ]; //包含源服务器中的源文件夹 string FTP_DISCH = System.Configuration.ConfigurationManager.AppSettings [FTP_DISCH]; // FTP路径(ftp:// *********** /)字符串USERNAME = System.Configuration.ConfigurationManager.AppSettings [USERNAME]; string PASSWORD = System.Configuration.ConfigurationManager.AppSettings [PASSWORD]; DirectoryInfo DISCH_Directory =新的DirectoryInfo(DISCH_DEST); FileInfo [] DISCH_Files = DISCH_Directory.GetFiles(*。*); foreach(DISCH_Files中的var f)//从BULK FOLDER(IN)中获取文件 { 字符串FN = Path.GetFileName(f.FullName); int bufferSize = 1024; FtpWebRequest REQ =(FtpWebRequest)WebRequest.Create(new Uri(String.Format({0} / {1},FTP_DISCH,FN))); REQ.Credentials = new NetworkCredential(USERNAME,PASSWORD); REQ.Method = WebRequestMethods.Ftp.UploadFile; 流FTP_Stream = REQ.GetRequestStream(); FileStream LOCAL_FileStream = new FileStream(f.FullName,FileMode.Create); byte [] bytebuffer = new byte [bufferSize]; int bytesSent = FTP_Stream.Read(bytebuffer,0,bufferSize); 尝试 { while(bytesSent!= 0) { LOCAL_FileStream.Write(bytebuffer,0,bytesSent); bytesSent = FTP_Stream.Read(bytebuffer,0,bytesSent); catch(Exception ex){Console.WriteLine(ex.ToString()); } LOCAL_FileStream.Close(); FTP_Stream.Close(); REQ = null; } }在许多文件中,这只是部分传输单个文件。例如。我有一个56KB的源文件。在运行下面的代码后,源文件将减少到0kb,并将0KB文件传输到目标文件,而不是56 KB的文件大小。
我构建了将所有文件从源文件传输到目标文件的代码。但是在传输上面的单个0KB文件后,它并没有进一步发展。
请帮助我。
解决您的代码:
流FTP_Stream = REQ.GetRequestStream(); FileStream LOCAL_FileStream = new FileStream(f.FullName,FileMode.Create); byte [] bytebuffer = new byte [bufferSize]; int bytesSent = FTP_Stream.Read(bytebuffer,0,bufferSize);您正在创建一个新的流,然后从ftp服务器读取它以放入它。 。
如果您发送的文件不是FileMode.Create,因为它会生成一个新文件,但是FileMode.Open。
你也一定会从LOCAL_FileStream中读取数据并写入FTP_STream ....
I need to transfer files on FTP from one server to other. I have used the below code.
Out of many files this is transferring only a single file partially. For ex. I have a source file of 56KB. After running the below code, the source file is reduced to 0kb and a 0KB file was transferred to the destination instead of 56 KB file size.
I built code to transfer all the files from source to destination. But its not progressing further after transferring a single 0KB file as above.
Please help me.
static void Main(string[] args) { string DISCH_DEST = System.Configuration.ConfigurationManager.AppSettings["DISCH_DEST"]; //Contains the source folder in source server string FTP_DISCH = System.Configuration.ConfigurationManager.AppSettings["FTP_DISCH"]; // FTP path (ftp://***********/) string USERNAME = System.Configuration.ConfigurationManager.AppSettings["USERNAME"]; string PASSWORD = System.Configuration.ConfigurationManager.AppSettings["PASSWORD"]; DirectoryInfo DISCH_Directory = new DirectoryInfo(DISCH_DEST); FileInfo[] DISCH_Files = DISCH_Directory.GetFiles("*.*"); foreach (var f in DISCH_Files) //FETCHING FILES FROM THE BULK FOLDER (IN) { string FN = Path.GetFileName(f.FullName); int bufferSize = 1024; FtpWebRequest REQ = (FtpWebRequest)WebRequest.Create(new Uri(String.Format("{0}/{1}",FTP_DISCH,FN))); REQ.Credentials = new NetworkCredential(USERNAME, PASSWORD); REQ.Method = WebRequestMethods.Ftp.UploadFile; Stream FTP_Stream = REQ.GetRequestStream(); FileStream LOCAL_FileStream = new FileStream(f.FullName, FileMode.Create); byte[] bytebuffer = new byte[bufferSize]; int bytesSent = FTP_Stream.Read(bytebuffer, 0, bufferSize); try { while (bytesSent != 0) { LOCAL_FileStream.Write(bytebuffer, 0, bytesSent); bytesSent = FTP_Stream.Read(bytebuffer, 0, bytesSent); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } LOCAL_FileStream.Close(); FTP_Stream.Close(); REQ = null; } }Out of many files this is transferring only a single file partially. For ex. I have a source file of 56KB. After running the below code, the source file is reduced to 0kb and a 0KB file was transferred to the destination instead of 56 KB file size.
I built code to transfer all the files from source to destination. But its not progressing further after transferring a single 0KB file as above.
Please help me.
解决方案Right now I dont see why your code would send any files.
Your code:
Stream FTP_Stream = REQ.GetRequestStream(); FileStream LOCAL_FileStream = new FileStream(f.FullName, FileMode.Create); byte[] bytebuffer = new byte[bufferSize]; int bytesSent = FTP_Stream.Read(bytebuffer, 0, bufferSize);You're making a new stream, and then reading from the ftp server to put in it...
If you were sending a file it wouldnt be FileMode.Create, as that makes a new file, but FileMode.Open.
You also surely would read from LOCAL_FileStream and write to FTP_STream....
更多推荐
FTP文件传输C#
发布评论