• asp.net通过ftp上传文件


     public byte[] FtpDownLoad(string filePath)
            {
                try
                {
                    FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer + ftpMapPath + filePath), WebRequestMethods.Ftp.DownloadFile);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    if (response == null)
                    {
                        return null;
                    }
                    Stream retStream = response.GetResponseStream();
                    List<byte> contentArray = new List<byte>();
                    int bt = retStream.ReadByte();
                    while (bt > -1)
                    {
                        contentArray.Add((byte)bt);
                        bt = retStream.ReadByte();
                    }
                    retStream.Dispose();
                    return contentArray.ToArray();
                }
                catch (Exception ex)
                {
                    LogHelper.LogError("FtpDownLoad失败!", ex);
                    return null;
                }
            }
            private FtpWebRequest GetFtpWebRequest(Uri uri, string method)
            {
                FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri);
    
                ftpClientRequest.Proxy = null;
                ftpClientRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                ftpClientRequest.UseBinary = true;
                //ftpClientRequest.UsePassive = true;
                ftpClientRequest.Method = method;
                //ftpClientRequest.Timeout = -1;
    
                return ftpClientRequest;
            }
            private bool FtpUploadFile(byte[] fileContent,string fullPath)
            {
                try
                {
                    string ftpFileDir = Path.GetDirectoryName(fullPath);
                    //这个总要执行
                    FtpMakeDirByPath(ftpFileDir.Replace("\","/"));
    
                    FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer+fullPath), WebRequestMethods.Ftp.UploadFile);
                    Stream stream = reqFTP.GetRequestStream();
                    stream.Write(fileContent, 0, fileContent.Length);
                    stream.Flush();
                    stream.Close();
                    return true;
                }
                catch(Exception ex)
                {
                    LogHelper.LogError("FtpUploadFile失败!", ex);
                    return false;
                }            
            }
            /// <summary>
            /// 根据路径创建多级目录
            /// </summary>
            /// <param name="dirPath"></param>
            /// <returns></returns>
            private bool FtpMakeDirByPath(string dirPath)
            {
                if(string.IsNullOrEmpty(dirPath))
                {
                    return false;
                }
                dirPath=dirPath.Replace("\", "/");
                string[] dirs= dirPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (dirs != null && dirs.Length > 0)
                {
                    bool result = false;
                    string des = fptServer;
                    try
                    {
                        for (int i = 0; i < dirs.Length; i++)
                        {
                            result = FtpMakeDir(des, dirs[i]);
                            des = des + "/" + dirs[i];
                        }
                        return true;
                    }
                    catch(Exception ex)
                    {
                        LogHelper.LogError("FtpMakeDirByPath失败!", ex);
                        return false;
                    }
                    
                }
                return false;
            }
            //创建目录
            private bool FtpMakeDir(string destinationPath,string dirName)
            {
                FtpWebResponse response = null;
                try
                {
                    Uri uri = new Uri(destinationPath+"/"+ dirName);
                    FtpWebRequest reqFTP = GetFtpWebRequest(uri, WebRequestMethods.Ftp.MakeDirectory);
                    response = (FtpWebResponse)reqFTP.GetResponse();
                    response.Close();
                    return true;
    
                }
                catch (Exception ex)
                {
                    LogHelper.LogError("FtpMakeDir失败!", ex); 
                    return false;
                }
            }        
    
  • 相关阅读:
    《免费:商业的未来》书摘
    wxWidgets的安装编译、相关配置、问题分析处理
    CodeBlocks的下载安装、配置、简单编程
    【最新】让快捷方式 实现相对路径——非.bat方式实现
    跨平台C/C++集成开发环境-Code::Blocks-内置GCC
    IOS自定义alertview
    IOS快速开发之常量定义
    为UITableViewController瘦身
    IOS中扩展机制Category和associative
    IOS制作一个漂亮的登录界面
  • 原文地址:https://www.cnblogs.com/AlanWinFun/p/5372722.html
Copyright © 2020-2023  润新知