本文介绍了如何使用C#代码将.txt文件从一个服务器上传到Ftp服务器到另一个文件夹到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是将文件上传到ftp服务器的代码,但它显示我找不到文件异常



FileInfo fileInf = new FileInfo(filename);





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





FtpWebRequest reqFTP;

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp://+ ftpServerIP + path +/+ filename)); ///+ awb +

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

reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

reqFTP.UseBinary = true;

reqFTP.Proxy = null;



reqFTP.ContentLength = fileInf.Length;



//缓冲区大小设置为2kb

int buffLength = 2048;

byte [] buff = new byte [buffLength];

int contentLen;



//打开文件流(System.IO.FileStream)来读取要上传的文件

FileStream fs = fileInf.OpenRead();



//使用(FileStream stream = File.Open(@D:\ SSIMFROMFTPServer \,FileMode.Create ));



//写入要上传的文件的流

Stream strm = reqFTP.GetRequestStream();



//测试

// FileStream writeStream = new FileStream(localDestnDir +/+ strm,FileMode.Create);



//一次从文件流中读取2kb

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



// Till Stream内容结束

while(contentLen!= 0)

{

//将内容从文件流写入FTP上传流

strm.Write(buff,0,contentLen);

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

}

//关闭文件流和请求流

strm.Close( );

fs.Close();

The below is code for uploading files to the ftp server but it shows me file not found exception

FileInfo fileInf = new FileInfo(filename);


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


FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + path + "/" + filename));//"/" + awb +
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;

reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();

// using (FileStream stream = File.Open(@"D:\SSIMFROMFTPServer\", FileMode.Create));

// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

//test
// FileStream writeStream = new FileStream(localDestnDir + "/" + strm, FileMode.Create);

// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();

推荐答案


这篇关于如何使用C#代码将.txt文件从一个服务器上传到Ftp服务器到另一个文件夹到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:10