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;
                    }
                   
  • 相关阅读:
    导包路径
    django导入环境变量 Please specify Django project root directory
    替换django的user模型,mysql迁移表报错 django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependen cy user.0001_initial on database 'default'.
    解决Chrome调试(debugger)
    check the manual that corresponds to your MySQL server version for the right syntax to use near 'order) values ('徐小波','XuXiaoB','男','1',' at line 1")
    MySQL命令(其三)
    MySQL操作命令(其二)
    MySQL命令(其一)
    [POJ2559]Largest Rectangle in a Histogram (栈)
    [HDU4864]Task (贪心)
  • 原文地址:https://www.cnblogs.com/lodestar/p/717110.html
Copyright © 2011-2022 走看看