목표: ftp protocol을 이용하여 지정된 host로부터 파일 다운로드 하여보자.

 

public bool DownloadFile(string sourcefile, string localfile, bool IsfileSizeChk)
{
int fsize=0;
FileInfo fi = new FileInfo(localfile);

// 1. check source
string target;
if (sourcefile.Trim() == "")
{
throw (new ApplicationException("File not Specified!"));
}
else if (sourcefile.Contains("/"))
{
//treat as a full path
target = AdjustDir(sourcefile);
}
else
target = "/" + sourcefile;

string URI = "ftp://" + axSinfo.hostname + "/" + target;

// 2. perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);

// set requeset to download a file in binary mode
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;

// open request and get response stream
try
{
using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
// loop to read & write to file
using (FileStream fs = fi.OpenWrite())
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
fsize +=read;
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
// catch error and delete file only partially dowmloaded
fs.Close();
// delete target files as it's incomplete
fi.Delete();
throw;
}
}

responseStream.Close();
}
response.Close();

// local PC로 download가 정상적으로 수행되었는지 file size를 Check한다.
CheckFileStatus(fsize, fi, IsfileSizeChk);
}
}
catch (WebException err)
{
string strErrMsg;
strErrMsg = string.Format(@"Window MSG:{0}

파일이 존재하는지 확인해 주십시요]
Download file name:{1} IP:{2}, USER ID:{3}]", err.ToString(),target, axSinfo.hostname, axSinfo.userid);
MessageBox.Show(strErrMsg,"Error Information!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


ftp = null;

return true;
}

 

file이 존재하는지 확인한다.

private void CheckFileStatus(int fsize, FileInfo fi, bool IsfileSizeChk)
{
if (IsfileSizeChk == true)
{
string strTemp;
if (fi.Exists == false)
MessageBox.Show("TESTER SIDE에 파일이 존재하지 않습니다. 확인 바랍니다.", "Error Information",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (fsize != fi.Length)
{
strTemp = string.Format("파일이 정상적으로 다운로드 되지 않았습니다. filesize [{0}] : [{1}]",
fsize, fi.Length);
MessageBox.Show(strTemp, "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
}

/를 씌우고, user name password를 지정한다.

/// Amend an FTP path so that it always starts with
private string AdjustDir(string path)
{
return ((path.StartsWith("/")) ? "" : "/").ToString() + path;
}

//Get the basic FtpWebRequest object with the
//common settings and security
private FtpWebRequest GetRequest(string URI)
{
//create request
FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
//Set the login details
result.Credentials = GetCredentials();
//Do not keep alive (stateless mode)
result.KeepAlive = false;

return result;
}

/// <summary>
/// Get the credentials from username/password
/// </summary>
private System.Net.ICredentials GetCredentials()
{
return new System.Net.NetworkCredential(axSinfo.userid, axSinfo.password);
}

 

+ Recent posts