当前位置:网站首页>Local file upload FTP or remote directory
Local file upload FTP or remote directory
2022-06-13 07:03:00 【AndroidOrCSharp】
1. Upload to FTP
/// <summary>
/// Upload files FTP
/// </summary>
/// <param name="localFile"></param>
/// <param name="workDate"></param>
/// <param name="batchNo"></param>
/// <returns></returns>
public bool UploadFiles1(string localFile,string workDate,string batchNo)
{
string FTPServer = string.Empty;
string FTPPort = string.Empty;
string FTPUser = string.Empty;
string FTPPwd = string.Empty;
string SendDir = string.Empty;
string dir = string.Empty;
FileStream fs = null;
Stream strm = null;
try
{
GetFTPServerCfg("0004", ref FTPServer, ref FTPPort, ref FTPUser, ref FTPPwd,ref SendDir);
FileInfo fileInf = new FileInfo(localFile);
dir = "ftp://" + FTPServer + "/" + SendDir + "/"+ workDate + "_" + batchNo;
string uri = "ftp://" + FTPServer + "/" + SendDir + "/" + workDate + "_" + batchNo +"/"+ fileInf.Name;
MakeDir(dir, reqFTP, FTPUser, FTPPwd);
Connect(uri, FTPUser, FTPPwd);
// The default is true, The connection won't be closed
// Executed after a command
reqFTP.KeepAlive = false;
// Specify what command to execute
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Notify the server of the size of the file when uploading it
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Open a file stream (System.IO.FileStream) To read the uploaded file
fs = fileInf.OpenRead();
// Write the uploaded file to the stream
strm = reqFTP.GetRequestStream();
// Every time I read the file stream kb
contentLen = fs.Read(buff, 0, buffLength);
// Stream content doesn't end
while (contentLen != 0)
{
// Take the content from file stream write in upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
return true;
}
catch (Exception ex)
{
//SystemMessge.Warning(ex.Message, " Upload error ");
return false;
}
finally
{
// Close both streams
if (strm != null)
{
strm.Close();
}
if (fs != null)
{
fs.Close();
}
}
}
/// <summary>
/// Create folder
/// </summary>
public void MakeDir(string uri, FtpWebRequest reqFTP, string ftpUserID, string ftpPassword,string fileName)
{
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);// Chinese file name
// Determine whether the folder exists Create... If it doesn't exist ( Only one level directory can be created )
string line = reader.ReadLine();
while (line != null)
{
if (line.Contains(fileName))
{
response.Close();
reader.Close();
return;
}
line = reader.ReadLine();
}
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UseBinary = true;
response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
reader.Close();
response.Close();
}
catch (Exception ex)
{
}
}
#region Connect to server
/// <summary>
/// Connect to server
/// </summary>
/// <param name="path">FTP Address </param>
/// <param name="ftpUserID"> user name </param>
/// <param name="ftpPassword"> password </param>
private void Connect(String path ,string ftpUserID,string ftpPassword)
{
// according to uri establish FtpWebRequest object
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// Specify the data transfer type
reqFTP.UseBinary = true;
// ftp User name and password
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
}
#endregion
2. Upload to remote directory
// Complete one TANO The corresponding instructions are uploaded to FTP
Dir += "\\" + WorkDate + "_" + BatchNo;
List<FileInfo> files = new List<FileInfo>();
// Get all the files that need to be uploaded under the folder
files.AddRange(new DirectoryInfo(Dir).GetFiles());
log.Info(string.Format(" Start to execute the function of uploading the local remittance instruction to the remote server "));
GetFTPServerCfg("0005", ref FTPServer, ref FTPPort, ref FTPUser, ref FTPPwd, ref SendDir);
SendDir = @"\\" + FTPServer + @"\" + SendDir + WorkDate + "_" + BatchNo;
// Determine whether the remote folder exists , Create if it does not exist
if (!Directory.Exists(SendDir))
{
Directory.CreateDirectory(SendDir);
}
foreach (FileInfo file in files)
{
UploadFiles(Dir+"\\"+file.Name,SendDir, file.Name);
}
/// <summary>
/// Upload files FTP
/// </summary>
/// <param name="localFile"></param>
/// <param name="SendDir"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public void UploadFiles(string localFile, string SendDir,string fileName)
{
// establish WebClient example
File.Copy(localFile, SendDir + "/" + fileName, true);
}
边栏推荐
- 杭州网上开户是安全的吗?
- 【騰訊阿裏最全面試題集錦】(四面:3輪技術+1輪HR)
- Why is the new e-commerce outlet mode so popular? What is the specific mode?
- Will the chain 2+1 model be a new business outlet and a popular Internet e-commerce market?
- [Markov chain Monte Carlo] Markov chain Monte Carlo method sampling prior distribution
- Ticdc synchronization task
- 简单了解C语言基本语
- 我的理财产品显示清算中是什么意思?
- Detailed Mr Atlas of hip joint (Reprinted)
- Monotone stack top31 of interview must brush algorithm top101
猜你喜欢

How to use Wangyou DFM software for cold plate analysis

Priority analysis of list variables in ansible playbook and how to separate and summarize list variables

10 Honest Facts I Want To Share With All Junior Developers

【RS-422与RS-485】RS-422与RS-485串行接口标准

Nfv basic overview

SDN basic overview

105. constructing binary trees from preorder and inorder traversal sequences

Xuanwu cloud technology passed the listing hearing: the performance fluctuated significantly, and chenyonghui and other three were the controlling shareholders
![[system analysis and design] college student association management system](/img/8f/a23dbdcba7c6afe7d7ce3abba2865c.jpg)
[system analysis and design] college student association management system

NFV基本概述
随机推荐
如何从头自己制作开发板?图文并茂,一步步操作给你看。
Tidb data migration (DM) Introduction
Eureka server multi node deployment
[system analysis and design] college student association management system
Is it safe for Hangzhou Securities to open an account?
2022-06-12:在N*N的正方形棋盘中,有N*N个棋子,那么每个格子正好可以拥有一个棋子。 但是现在有些棋子聚集到一个格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二维数组代表,一
Host computer development (Architecture Design of firmware download software)
在产业互联网的概念刚刚出现时,人们仅仅只是将其看成是一个获取B端流量的方法
Tidb statistics
DM Experiment 6: filter replication
TiDB Lightning
【马尔科夫链-蒙特卡罗】马尔科夫链-蒙特卡罗方法对先验分布进行抽样
Detailed Mr Atlas of hip joint (Reprinted)
SDN basic overview
Through the function seaborn cubehelix_ Palette build order palette
Implementation of fruit mall wholesale platform based on SSM
As the new trend of blind box e-commerce, how can the platform use blind box play to drain at low cost?
数字时代进化论
How to write an amazing design document?
15、 IO stream (I)