c#使用FTP协议将文件上传至unix主机

ShaneGK 2019-01-21 08:56:33
该程序原本是从windows主机A得到指定excel文档, 处理后发送邮件并上传至另一台Windows主机B, 现在主机B要变成Unix主机,并使用FTP协议,
我在程序里找到如下上传方法,应该是Windows主机的吧..., 请问大神如果变成上传至Unix主机要怎么写呢?谢谢各位大神!!

 public string Upload(string FTPSubPath, string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + FTPSubPath + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
try
{
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Proxy = null; //当你的机子使用的是代理上网时,最好加上这一行,要不然报“使用 HTTP 代理时不支持请求的 FTP 命令。”错误

// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;

// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// Specify the data transfer type.
reqFTP.UseBinary = true;

// Notify the server about the size of the uploaded file
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();
FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

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

// 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();
return "ok";
}
catch (Exception ex)
{
reqFTP.Abort();
// Logging.WriteError(ex.Message + ex.StackTrace);
return ex.ToString() ;
}

}



~~~~~~~~~~~调用上面Upload方法的地方~~~~~~~~~~~~~~~~`

   if (attachFiles.Length > 0)
{
foreach (FileInfo attachName in oldFiles)
{
string rslt=ftpHelp_SPIL.Upload("/Jessie Test", attachName.Name);
if (rslt != "ok")
{
MailAbout.sendMail(strFrom, "SPILSZ", ini.Err_To, ini.Err_To, "FTP上传失败", ini.ErrSubject, sLocalOutputFilePath); ;
}
}
}

...全文
65 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
ftp是一种应用,上传到ftp服务器之后的服务器操作系统等等对上传本身没有任何影响。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧