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;
                }
               

posted on 2007-04-17 17:21  lodestar  阅读(4373)  评论(2编辑  收藏  举报

导航