zhu894532094 发表于 2015-5-27 09:51:38

FTP图片上传到异地服务器

#region 创建文件夹MakeFolder()
    ///   
    /// 创建文件夹   
    ///   
    /// 例如:/newDir   
    /// ftp地址(可带目录)
    /// ftp用户名
    /// ftp密码
    public void MakeFolder(string FolderName, string url, string userid, string password)
    {
      FtpWebRequest reqFTP;
      try
      {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + url + FolderName));
            reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(userid, password);
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            ftpStream.Close();
            response.Close();
      }
      catch (Exception ex)
      {
            throw new Exception("此处是创建文件夹:"+ex.Message + "创建文件夹:ftp://" + url + FolderName + "不成功!");
      }
    }
///
    /// 生成缩略图
    ///
    /// 数据流
    /// 缩略图路径(ftp相对路径)
    /// 缩略图宽度
    /// 缩略图高度
    /// 生成缩略图的方式   
    public void MakeThumbnail(Stream stream, string thumbnailPath, int width, int height, string mode)
    {
      System.Drawing.Image originalImage = System.Drawing.Image.FromStream(stream);
      int towidth = width;
      int toheight = height;
      int x = 0;
      int y = 0;
      int ow = originalImage.Width;
      int oh = originalImage.Height;
      switch (mode)
      {
            case "HW"://指定高宽缩放(可能变形)               
                break;
            case "W"://指定宽,高按比例                     
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://指定高,宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://指定高宽裁减(不变形)               
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                  oh = originalImage.Height;
                  ow = originalImage.Height * towidth / toheight;
                  y = 0;
                  x = (originalImage.Width - ow) / 2;
                }
                else
                {
                  ow = originalImage.Width;
                  oh = originalImage.Width * height / towidth;
                  x = 0;
                  y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
      }
      //新建一个bmp图片
      System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
      //新建一个画板
      Graphics g = System.Drawing.Graphics.FromImage(bitmap);
      //设置高质量插值法
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
      //设置高质量,低速度呈现平滑程度
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
      //清空画布并以透明背景色填充
      g.Clear(Color.Transparent);
      //在指定位置并且按指定大小绘制原图片的指定部分
      g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
            new Rectangle(x, y, ow, oh),
            GraphicsUnit.Pixel);
      try
      {
            MemoryStream temp = new MemoryStream();
            bitmap.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
            temp.Position = 0;
            
            UploadFileByFTP(this.ftpHost, this.ftpUserId, this.ftpPwd, temp, thumbnailPath);
      }
      catch (System.Exception e)
      {
            throw new Exception("此处是生成缩略图:"+e.Message+"生成缩略图失败!");
      }
      finally
      {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
      }
    }
#region FTP上传文件方法,设定ftp下的保存路径,文件名UploadFileByFTP()
    ///
    /// FTP上传文件方法,设定ftp下的保存路径,文件名
    ///
    /// ftp地址
    /// ftp用户名
    /// ftp密码
    /// 上传文件流
    /// 文件名(可带相对路径)
    /// throw
    protected void UploadFileByFTP(string ftpServerIP, string ftpUserID, string ftpPassword, Stream streamRead, string filename)
    {
      string l_notice = "";//上传提示
         string uri = string.Empty;
      //如果filename为空提示选择文件
      if (filename == null || filename == "")
      {
            l_notice = "Please select File !";
      }
      else
      {
            try
            {
               
                if (filename.StartsWith("/"))
                {
                  filename = filename.Remove(0, 1);
                }
                uri = "ftp://" + ftpServerIP + "/" + filename;
                FtpWebRequest reqFTP;
                // 根据uri创建FtpWebRequest对象
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                // ftp用户名和密码
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                // 默认为true,连接不会被关闭
                // 在一个命令之后被执行
                reqFTP.KeepAlive = false;
                // 指定执行命令
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                // 指定数据传输类型
                reqFTP.UseBinary = true;
                // 上传文件时通知服务器文件的大小
                reqFTP.ContentLength = streamRead.Length;
                // 缓冲大小设置为5kb
                int buffLength = 5048;
                byte[] buff = new byte;
                int contentLen;
                Stream streamWrite = reqFTP.GetRequestStream();
                streamRead.Flush();
                contentLen = streamRead.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                  streamWrite.Write(buff, 0, contentLen);
                  contentLen = streamRead.Read(buff, 0, buffLength);
                }
                // 关闭两个流
                streamWrite.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("此处是上传方法:"+ex.Message + "ftp路径:" + uri);
            }
      }
      Response.Write(l_notice);
    }
页: [1]
查看完整版本: FTP图片上传到异地服务器