如何使用C#代码将.txt文件从一个服务器上传到Ftp服务器到另一个文件夹到另一个文件夹 [英] How Can I Upload .Txt Files To Ftp Server From One Server To Folder To The Other Folder Using C# Code
问题描述
以下是将文件上传到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();
推荐答案
使用WinSCP;
namespace Project
{
class Upload_Files
{
public void upload_file_sftp()
{
试试
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName =abc.com,
UserName = abc,
密码=1234,
PortNumber = 22,
SshHostKeyFingerprint =ssh-rsa 1024 a7:20:03 :28:19:3C:88:4C:05:3F:CF:C3 :15:ad:57:20
};
使用(Session session = new Session())
{
;
session.Open(sessionOptions ); $
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(@P:\ File_upload,/ name_of_sftpfolder /,false,transferOptions);
transferResult.Check();
foreach(transferRventArgs transfer in transferResult.Transfers)
{
MessageBox.Show(上传文件+ transfer.FileName +成功);
transferResult.Transfers.Count();
}
}
}
catch(exception ex)
{
MessageBox.Show(例外:+ ex.Message);
}
}
}
}
注意:对于上面的代码,你需要添加winscp dll和exe。
using WinSCP;
namespace Project
{
class Upload_Files
{
public void upload_file_sftp()
{
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "abc.com",
UserName = "abc",
Password = "1234",
PortNumber = 22,
SshHostKeyFingerprint = "ssh-rsa 1024 a7:20:03:28:19:3c:88:4c:05:3f:cf:c3:15:ad:57:20"
};
using (Session session = new Session())
{
;
session.Open(sessionOptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(@"P:\File_upload","/name_of_sftpfolder/", false, transferOptions);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
MessageBox.Show("Upload of files " + transfer.FileName + " succeeded");
transferResult.Transfers.Count();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.Message);
}
}
}
}
Note: For above code u need to add winscp dll and exe.
这篇关于如何使用C#代码将.txt文件从一个服务器上传到Ftp服务器到另一个文件夹到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!