首页 > 代码库 > 文件复制,遍历,ftp下载

文件复制,遍历,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下载