如何用IIS搭建FTP服务器

1. 创建ftp站点

Windows2003默认情况下不安装IIS组件,需要手工安装,具体操作步骤如下:

开始→设置→控制面板→添加或删除程序→添加/删除windows组件→应用程序服务器

→详细信息→internet信息服务(IIS)→详细信息

 →文件传输协议(FTP)服务(选中该选项)→确定

   前提条件:光驱中放入Windows2003操作系统.

2. 启动FTP站点,设置相关参数

打开IIS服务器,具体操作步骤如下:

开始 →运行  →打开(在输入框中键入“inetmgr”命令)

弹出Internet信息服务(IIS)管理器,点击FTP站点,在右边会出现默认FTP站点,单击鼠标右键弹出下拉菜单,然后单击“属性”,在弹出的对话框中设置FTP的IP地址和TCP的端口号,没,默认情况下为端口号为21。并可以设置FTP站点的连接限制。

 

图1  右键单击【默认FTP站点】选项

    第2步,打开“默认FTP站点 属性”对话框,在【FTP站点】选项卡中可以设置关于FTP站点的参数。其中在“FTP站点标识”区域中可以更改FTP站点名称、监听IP地址以及TCP端口号,单击【IP地址】编辑框右侧的下拉三角按钮,并选中该站点要绑定的IP地址。如果想在同一台物理服务器中搭建多个FTP站点,那么需要为每一个站点指定一个IP地址,或者使用相同的IP地址且使用不同的端口号。在“FTP站点连接”区域可以限制连接到FTP站点的计算机数量,一般在局域网内部设置为【不受限制】较为合适。用户还可以单击【当前会话】按钮来查看当前连接到FTP站点的IP地址,并且可以断开恶意用户的连接,如图2所示。

 

图2   选择FTP站点IP地址

    第3步,切换到【安全账户】选项卡,此选项卡用于设置FTP服务器允许的登录方式。默认情况下允许匿名登录,如果取消选中【允许匿名连接】复选框,则用户在登录FTP站点时需要输入合法的用户名和密码。本例选中【允许匿名连接】复选框,如图3所示。

 

图3   选中【允许匿名连接】复选框

小提示:登录FTP服务器的方式可以分为两种类型:匿名登录和用户登录。如果采用匿名登录方式,则用户可以通过用户名“anonymous”连接到FTP服务器,以电子邮件地址作为密码。对于这种密码FTP服务器并不进行检查,只是为了显示方便才进行这样的设置。允许匿名登录的FTP服务器使得任何用户都能获得访问能力,并获得必要的资料。如果不允许匿名连接,则必须提供合法的用户名和密码才能连接到FTP站点。这种登录方式可以让管理员有效控制连接到FTP服务器的用户身份,是较为安全的登录方式。

    第4步,切换到【消息】选项卡,在【标题】编辑框中输入能够反映FTP站点属性的文字(如“金手指FTP主站点”),该标题会在用户登录之前显示。接着在【欢迎】编辑框中输入一段介绍FTP站点详细信息的文字,这些信息会在用户成功登录之后显示。同理,在【退出】编辑框中输入用户在退出FTP站点时显示的信息。另外,如果该FTP服务器限制了最大连接数,则可以在【最大连接数】编辑框中输入具体数值。当用户连接FTP站点时,如果FTP服务器已经达到了所允许的最大连接数,则用户会收到“最大连接数”消息,且用户的连接会被断开,如图4所示。

 

图4   【消息】选项卡

    第5步,切换到【主目录】选项卡。主目录是FTP站点的根目录,当用户连接到FTP站点时只能访问主目录及其子目录的内容,而主目录以外的内容是不能被用户访问的。主目录既可以是本地计算机磁盘上的目录,也可以是网络中的共享目录。单击“浏览”按钮在本地计算机磁盘中选择要作为FTP站点主目录的文件夹,并以此单击【确定】按钮。根据实际需要选中或取消选中【写入】复选框,以确定用户是否能够在FTP站点中写入数据,如图5所示。

 

图5   【主目录】选项卡

    小提示:如果选中【另一台计算机上的目录】单选钮,则【本地路径】编辑框将更改成【网络共享】编辑框。用户需要输入共享目录的UNC路径,以定位FTP主目录的位置。

第6步,切换到【目录安全性】选项卡,在该选项卡中主要用于授权或拒绝特定的IP地址连接到FTP站点。例如只允许某一段IP地址范围内的计算机连接到FTP站点,则应该选中【拒绝访问】单选钮。然后单击【添加】按钮,在打开的【授权访问】对话框中选中【一组计算机】单选钮。然后在【网络标识】编辑框中输入特定的网段(如10.115.223.0),并在【子网掩码】编辑框中输入子网掩码(如255.255.254.0)。最后单击【确定】按钮,如图6所示。

 

图6  【授权访问】对话框

    第7步,返回“默认FTP站点 属性”对话框,单击【确定】按钮使设置生效。现在用户已经可以在网络中任意客户计算机的Web浏览器中输入FTP站点地址(如http://www.cnblogs.com/xiayang/admin/ftp://10.115.223.60/)来访问FTP站点的内容了。

    小提示:如果FTP站点所在的服务器上启用了本地连接的防火墙,则需要在“本地连接 属性”的“高级设置”对话框中添加“例外”选项,否则客户端计算机不能连接到FTP站点。

 

事例:写日志并上传到FTP服务器

代码
       private   readonly   string  path  =   " F:\\ftproot\\ " ;
        
private   readonly   string  host  =   " 192.168.138.15 " ;
        
private   readonly   int  port  =   2121 ;
        
private   readonly   string  username  =   " ftp " ;
        
private   readonly   string  password  =   " ftp " ;
        
private   static   string  mcDBConnectString  =   " Data Source=192.168.138.104\\SQL2005;Initial Catalog=UAP5MC;User ID=sa;Password=password " ;

        
private   sealed   class  ScomServers
        {
            
public   string  Name {  get set ; }
            
public   string  Guid {  get set ; }

        }

        
private   void  button1_Click( object  sender, EventArgs e)
        {
// 生成scomlog
            generateLog();
        }

        
private   void  button2_Click( object  sender, EventArgs e)
        {
// 上传日志到FTP服务器
            uploadLogs();
        }

        
private  List < ScomServers >  loadScomServers()
        {
            List
< ScomServers >  servers  =   new  List < ScomServers > ();
            
string  sql  =   " select s.note + '.' + s.scomserverdomain as [Name], c.guidbyscom as Guid from tblscomServerInfo s, tblcmbase_server c where s.note = c.servername AND c.guidbyscom is not null "
            IDataReader reader 
=  GetDataReader(sql);

            
try
            {
                
while  ( true   ==  reader.Read())
                {
                    servers.Add(
new  ScomServers()
                    {
                        Name 
=  reader[ " Name " ].ToString(),
                        Guid 
=  reader[ " Guid " ].ToString(),
                    });
                }
            }
            
finally
            {
                reader.Close();
            }

            
return  servers;
        }


        
private   void  generateLog()
        {
            List
< ScomServers >  servers  =  loadScomServers();
            
string  date  =   string .Format( " {0:yyyyMMddHHmmssfffffff} " , DateTime.Now);  //  fixme: date format
            DateTime update  =  DateTime.Now.AddDays( - 1 );
            DateTime from 
=   new  DateTime(update.Year, update.Month, update.Day);
            DateTime to 
=  DateTime.Today.Date;


            
try
            {
                
if  (servers  !=   null )
                {
                    
foreach  (ScomServers server  in  servers)
                    {

                        
string  sql  =   @" select d.serverguid,d.updatetime as [DateTime],
                                    replace(d.servername,'Microsoft.Windows.Computer:','') as ComputerName,
                                    a.servermgip as ComputerIP,
                                    d.cpu as CPUUsedValue,
                                    d.memoryuse*100 as AvailablePer,
                                    'ALL' as DiskName,
                                    (1-d.storageuse) * 100 as FreeSpace,
                                    (d.TotalDiskArray - d.DiskArrayUse) as FreeSpaceMB
                                from tblscomServerPerformanceDetail d 
                                     left join tblcmbase_Server a
                                on d.serverguid = a.guidbyscom
                                where d.serverguid = '
"   +  server.Guid  +   " ' " ;
                               
                        IDataReader reader 
=  GetDataReader(sql);
                        
try
                        {
                            StreamWriter cpuWriter 
=  fileOpen( string .Join( "" new   string [] { path, server.Name,  " _ " , date,  " _SCOM_2007_CPU.log "  }),  string .Join( "" new   string [] {  " DateTime " " \t " " ComputerName " " \t " " ComputerIP " " \t " " CPUUsedValue " " \t " " PRI "  }));
                            StreamWriter ramWriter 
=  fileOpen( string .Join( "" new   string [] { path, server.Name,  " _ " , date,  " _SCOM_2007_Memory.log "  }),  string .Join( "" new   string [] {  " DateTime " " \t " " ComputerName " " \t " " ComputerIP " " \t " " AvailableMB " " \t " " PRI "  }));
                            StreamWriter hddWriter 
=  fileOpen( string .Join( "" new   string [] { path, server.Name,  " _ " , date,  " _SCOM_2007_Disk.log "  }),  string .Join( "" new   string [] {  " DateTime " " \t " " ComputerName " " \t " " ComputerIP " " \t " " DiskName " " \t " " FreeSpace " " \t " " FreeMB " " \t " " PRI "  }));

                            
while  ( true   ==  reader.Read())
                            {

                                cpuWriter.WriteLine(
string .Join( "" new   string [] {
                                reader[
" DateTime " ==   null   ?   ""  : reader[ " DateTime " ].ToString().Trim(),
                                 
" \t " ,
                                reader[
" ComputerName " ==   null   ?   ""  : reader[ " ComputerName " ].ToString().Trim(),
                                 
" \t " ,
                                reader[
" ComputerIP " ==   null   ?   ""  : reader[ " ComputerIP " ].ToString().Trim(),
                                 
" \t " ,
                                reader[
" CPUUsedValue " ==   null   ?   ""  : reader[ " CPUUsedValue " ].ToString().Trim(),
                                 
"   " ,
                                
" " ,
                            }));

                                ramWriter.WriteLine(
string .Join( "" new   string [] {
                                reader[
" DateTime " ==   null   ?   ""  : reader[ " DateTime " ].ToString().Trim(),
                                
" \t " ,
                                reader[
" ComputerName " ==   null   ?   ""  : reader[ " ComputerName " ].ToString().Trim(),
                                
" \t " ,
                                reader[
" ComputerIP " ==   null   ?   ""  : reader[ " ComputerIP " ].ToString().Trim(),
                                
" \t " ,
                                reader[
" AvailablePer " ==   null   ?   ""  : reader[ " AvailablePer " ].ToString().Trim(),
                                
" \t " ,
                                
//  fixme: ram data
                                 " " ,
                            }));
                                hddWriter.WriteLine(
string .Join( "" new   string [] { 
                                reader[
" DateTime " ==   null   ?   ""  : reader[ " DateTime " ].ToString().Trim(),
                                 
" \t " ,
                                reader[
" ComputerName " ==   null   ?   ""  : reader[ " ComputerName " ].ToString().Trim(),
                                 
"   " ,
                                reader[
" ComputerIP " ==   null   ?   ""  : reader[ " ComputerIP " ].ToString().Trim(),
                                 
" \t " ,
                                
" ALL " ,
                                reader[
" FreeSpace " ==   null   ?   ""  : reader[ " FreeSpace " ].ToString().Trim(),
                                 
" \t " ,
                                reader[
" FreeSpaceMB " ==   null   ?   ""  : reader[ " FreeSpaceMB " ].ToString().Trim(),
                                 
" \t " ,
                                
//  fixme: hdd data
                                 " " ,
                            }));
                            }

                            fileClose(cpuWriter);
                            fileClose(ramWriter);
                            fileClose(hddWriter);
                        }
                        
finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
            
catch  (Exception ex)
            {
            }
        }

        
private  StreamWriter fileOpen( string  fileName,  string  header)
        {
            StreamWriter writer 
=   new  StreamWriter(fileName,  true , System.Text.Encoding.UTF8);

            writer.WriteLine(header);
            
return  writer;
        }

        
private   void  fileClose(StreamWriter writer)
        {
            writer.Flush();
            writer.Close();
        }

        
private   void  uploadLogs()
        {
            
try
            {
                FtpClient ftp 
=   new  FtpClient(host, port);

                ftp.Open(username, password);

                ftp.ChangeDirectory(
" DB " );

                
string [] filenames  =  Directory.GetFiles(path,  " *.log " );
                
if  (filenames  !=   null   &&  filenames.Length  >   0 )
                {
                    
foreach  ( string  filename  in  filenames)
                    {
                        
if  (filename.Contains( " CPU " ))
                        {
                            ftp.ChangeDirectory(
" CPU " );
                        }
                        
else   if  (filename.Contains( " Memory " ))
                        {
                            ftp.ChangeDirectory(
" MEM " );
                        }
                        
else   if  (filename.Contains( " Disk " ))
                        {
                            ftp.ChangeDirectory(
" HDD " );
                        }

                        
string  fn  =  Path.GetFileName(filename);

                        
if  (ftp.Exists(Path.GetFileName(fn)))
                        {
                            ftp.DeleteFile(fn);
                        }

                        ftp.PutFile(filename);

                        File.Delete(filename);
                        ftp.ChangeDirectoryUp();
                    }
                }
                
else
                {
                }


                ftp.Close();

            }
            
catch  (Exception ex)
            {
            }
        }

        
#region  数据库操作

        

        
///   <summary>
        
///  获取数据DataSet
        
///   </summary>
        
///   <param name="sqlString"> 查询SQL </param>
        
///   <returns> 结果DataSet </returns>
         internal   static  DataSet GetDataSet( string  sqlString)
        {
            
// return Framework.IDE.DB.ExecuteDataSet(CommandType.Text, sqlString);
            DataSet ds  =   new  DataSet();
            SqlConnection conn 
=   new  SqlConnection(mcDBConnectString);
            SqlDataAdapter adp 
=   new  SqlDataAdapter(sqlString, conn);
            adp.Fill(ds);
            
return  ds;
        }

        
///   <summary>
        
///  获取数据DataSet
        
///   </summary>
        
///   <param name="sqlString"> 查询SQL </param>
        
///   <returns> 结果DataSet </returns>
         internal   static   object  GetDataScalar( string  sqlString)
        {
            
object  result  =   null ;
            
// result = Framework.IDE.DB.ExecuteScalar(CommandType.Text, sqlString);
            SqlConnection conn  =   new  SqlConnection(mcDBConnectString);
            
try
            {
                conn.Open();
                SqlCommand cmd 
=  conn.CreateCommand();
                cmd.CommandText 
=  sqlString;
                cmd.CommandType 
=  System.Data.CommandType.Text;
                result 
=  cmd.ExecuteScalar();
            }
            
finally
            {
                conn.Close();
            }

            
return  result;
        }

        
internal   static  IDataReader GetDataReader( string  sqlString)
        {
            IDataReader reader 
=   null ;

            SqlConnection conn 
=   new  SqlConnection(mcDBConnectString);
            
try
            {
                conn.Open();
                SqlCommand cmd 
=  conn.CreateCommand();
                cmd.CommandText 
=  sqlString;
                cmd.CommandType 
=  System.Data.CommandType.Text;
                reader 
= (IDataReader) cmd.ExecuteReader();
            }
            
finally
            {
               
            }
            
return  reader;
        }

        
///   <summary>
        
///  获取数据DataSet
        
///   </summary>
        
///   <param name="sqlString"> 查询SQL </param>
        
///   <returns> 结果DataSet </returns>
         internal   static   int  ExecuteNonQuery( string  sqlString)
        {
            
int  result  =   0 ;
            
//  result = Framework.IDE.DB.ExecuteNonQuery(CommandType.Text, sqlString);
            SqlConnection conn  =   new  SqlConnection(mcDBConnectString);
            
try
            {
                conn.Open();
                SqlCommand cmd 
=  conn.CreateCommand();
                cmd.CommandText 
=  sqlString;
                cmd.CommandType 
=  System.Data.CommandType.Text;
                result 
=  cmd.ExecuteNonQuery();
            }
            
finally
            {
                conn.Close();
            }

            
return  result;
        }
        
#endregion

 

注:

Starksoft.Net.Ftp.dll
Starksoft.Net.Proxy.dll
文件上传到FTP服务器用到的两个DLL

你可能感兴趣的:(IIS)