zoukankan      html  css  js  c++  java
  • FtpWebResponse实现ftp文件上传碰到的小问题

    FtpWebRequest上传文件报550错误
    按照一般的思路做两件事情
    1.防火墙的设置,验证没有问题
    2.权限设计,ftp的上传权限也没有问题

     public string Put()
            
    {
                
    try
                
    {
                    
    //
                    
    //  打开上传文件
                    
    //
                    FileStream sourceStream = new FileStream(m_Filename, FileMode.Open);

                    FileInfo fileInf 
    = new FileInfo(m_Filename);

                    
    //
                    
    //  绑定Url 例如: ftp://host/path
                    
    //
                    UriBuilder ftpUri = new UriBuilder("ftp", m_Host);
                    
    if ((m_Destination == null|| (m_Destination.Length == 0))
                    
    {
                        ftpUri.Path 
    = m_Filename;
                    }

                    
    else
                    
    {
                        ftpUri.Path 
    = m_Destination + "/" + fileInf.Name;
                    }


                    
    //
                    
    //  创建ftp连接
                    
    //
                    m_Ftp = (FtpWebRequest)WebRequest.Create(ftpUri.Uri);
                    m_Ftp.Method 
    = WebRequestMethods.Ftp.UploadFile;
                    m_Ftp.UseBinary 
    = m_UseBinary;
                    m_Ftp.UsePassive 
    = m_UsePassive;
                    m_Ftp.Credentials 
    = new NetworkCredential(m_Username, m_Password);
                    FtpWebResponse response 
    = (FtpWebResponse)m_Ftp.GetResponse();
                    Console.WriteLine(response.BannerMessage);

                    ftpstring.AppendLine(response.BannerMessage);
                    Stream ftpStream 
    = m_Ftp.GetRequestStream();

                    
    //
                    
    //  读数据并写入FtpStream
                    
    //
                    int bufferSize = 8192;
                    
    int readCount;
                    
    byte[] buffer = new byte[bufferSize];

                    readCount 
    = sourceStream.Read(buffer, 0, bufferSize);
                    
    while (readCount > 0)
                    
    {
                        ftpStream.Write(buffer, 
    0, readCount);
                        readCount 
    = sourceStream.Read(buffer, 0, bufferSize);
                    }


                    
    //
                    
    //  关闭FtpStream
                    
    //
                    sourceStream.Close();
                    ftpStream.Close();

                    
    //
                    
    //  得到最终ftp结果
                    
    //
                    Console.WriteLine(response.StatusDescription);

                    ftpstring.AppendLine(response.StatusDescription);

                    response.Close();

                    
    return ftpstring.ToString();

                }

                
    catch (Exception ex)
                
    {
                    Console.WriteLine(
    "Exception during FtpPut Activity{0}{1}",
                        Environment.NewLine,
                        ex);
                    ftpstring.AppendLine(
    "发送错误" + Environment.NewLine+ex.Message);

                    
    return ftpstring.ToString();
                }

            }

    后来在微软的中文社区看到了如下代码

             

     FileInfo fileInf = new FileInfo(filename);
              
    string uri = "ftp://" + m_Host + "/" + m_Destination + "/" + fileInf.Name;
              FtpWebRequest reqFTP;
                
              
    // Create FtpWebRequest object from the Uri provided
              reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
                        
    "ftp://" + m_Host + "/" + m_Destination + "/" + fileInf.Name));

              
    // Provide the WebPermission Credintials
              reqFTP.Credentials = new NetworkCredential(m_Username, 
                                                         m_Password);
                
              
    // By default KeepAlive is true, where the control connection is 
              
    // not closed after a command is executed.
              reqFTP.KeepAlive = false;

              
    // Specify the command to be executed.
              reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                
              
    // Specify the data transfer type.
              reqFTP.UseBinary = true;

              
    // Notify the server about the size of the uploaded file
              reqFTP.ContentLength = fileInf.Length;

              
    // The buffer size is set to 2kb
              int buffLength = 2048;
              
    byte[] buff = new byte[buffLength];
              
    int contentLen;
                
              
    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
              FileStream fs = fileInf.OpenRead();
               
              
    try
              
    {
                    
    // Stream to which the file to be upload is written
                    Stream strm = reqFTP.GetRequestStream();
                    
                    
    // Read from the file stream 2kb at a time
                    contentLen = fs.Read(buff, 0, buffLength);
                    
                    
    // Till Stream content ends
                    while (contentLen != 0)
                    
    {
                        
    // Write Content from the file stream to the 
                        
    // FTP Upload Stream
                        strm.Write(buff, 0, contentLen);
                        contentLen 
    = fs.Read(buff, 0, buffLength);
                    }

                    
                    
    // Close the file stream and the Request Stream
                    strm.Close();
                    fs.Close();
              }

              
    catch(Exception ex)
                
    {
                     Console.WriteLine(ex.Message, 
    "Upload Error");
                }

            } 

    想了一下终于明白了,原来FTP的目标路径要加入文件,想想也是,不然怎么建立文件流呢?
    这段代码改成这样,哈哈ok了。
     

    if ((m_Destination == null|| (m_Destination.Length == 0))
                    
    {
                        ftpUri.Path 
    = m_Filename;
                    }

                    
    else
                    
    {
                        ftpUri.Path 
    = m_Destination + "/" + fileInf.Name;
                    }
                   
  • 相关阅读:
    《Programming WPF》翻译 第8章 1.动画基础
    一些被遗忘的设计模式
    《Programming WPF》翻译 第4章 数据绑定
    《Programming WPF》翻译 第3章 控件
    《Programming WPF》翻译 第5章 样式和控件模板
    《Programming WPF》翻译 第7章 绘图
    《Programming WPF》翻译 第9章 自定义控件
    《Programming WPF》翻译 第7章 绘图 (2)
    《Programming WPF》翻译 第8章 前言
    关于Debug和Release之本质区别
  • 原文地址:https://www.cnblogs.com/lodestar/p/717110.html
Copyright © 2011-2022 走看看