文件复制,遍历,ftp下载

Posted ningmou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件复制,遍历,ftp下载相关的知识,希望对你有一定的参考价值。

技术分享
public partial class WebPp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string path = Request.PhysicalApplicationPath + "WebPrep/PrepSetp/" +"aa.pdf";

            if (!File.Exists(path))
            {
                if (DownLoadPdf())//PDF下载成功
                {
                    //将下载的pdf复制到IIS访问目录下
                    FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + Request.QueryString["PdfName"]);
                    FileInfo fileDest = new FileInfo(path);
                    if (file.Exists)
                    {
                        if (Directory.Exists(fileDest.DirectoryName) == false)
                        {
                            Directory.CreateDirectory(fileDest.DirectoryName);
                        }
                        else
                        {
                            DirectoryInfo fld = new DirectoryInfo(fileDest.DirectoryName);
                            FileInfo[] fileInfo = fld.GetFiles();
                            foreach (FileInfo NextFile in fileInfo)  //遍历文件
                            {
                                try
                                {//删除访问日期7天前的数据
                                    if (NextFile.LastAccessTime < DateTime.Now.AddDays(-7))
                                    {
                                        NextFile.Delete();
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }                     

                        file.CopyTo(path, true);
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    /// <summary>
    /// 添加下载锁 
    /// </summary>
    private static readonly object FTPDownLoad = new object();
    private bool DownLoadPdf()
    {
        if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["FilePath"])|| string.IsNullOrEmpty(Request.QueryString["PdfName"]))
            return false;

        //实例化一个文件对象
        FileInfo fileInfo = null;
        fileInfo = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + Request.QueryString["PdfName"].ToString());
        FtpClient ftp = new FtpClient();

        //如果文件存在
        if (fileInfo.Exists == true && fileInfo.Length > 0)
            return true;

        lock (FTPDownLoad)
        {
            //如果文件为空
            try
            {
                string ftpServer = ConfigurationManager.AppSettings["LBFTPServer"];
                string ftpPort = ConfigurationManager.AppSettings["LBFTPPort"];
                string ftpUserName = ConfigurationManager.AppSettings["LBFTPUserName"];
                string ftpPassword = ConfigurationManager.AppSettings["LBFTPPassword"];
                ftp.Server = ftpServer;
                ftp.Port = int.Parse(ftpPort);
                ftp.Username = ftpUserName;
                ftp.Password = ftpPassword;
                ftp.ChangeDir(fileInfo.DirectoryName.Split(\\)[fileInfo.DirectoryName.Split(\\).Length - 1]);
                if (Directory.Exists(fileInfo.DirectoryName) == false)
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                ftp.Download(fileInfo.Name, fileInfo.FullName, true);
                return true;
            }
            catch (Exception ex)
            {
                SunWin.Common.CommonUtil.LogHelper.Error(Session["CurrentUser"].ToString(), GetClientIp.GetClientIP(Context), "附件通用下载页,FTP方法", ex.Message);
            }
            finally
            {
                ftp.Close();
            }
        }
    }


    /// <summary>
    /// 方法重写,解决控件必须放在具有 runat=server 的窗体标记内
    /// </summary>
    /// <param name="control"></param>
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an htmlForm control is rendered for 
    }
}
  
View Code

 

以上是关于文件复制,遍历,ftp下载的主要内容,如果未能解决你的问题,请参考以下文章

java Ftp上传创建多层文件的代码片段

FTPSFTP文件下载内容校验

怎么远程用ftp命令把文件下载到本地

怎么从linux下载文件到win中?

如何把FTP中的文件复制到本地文件夹

pythonftp给文件授权