配置 FTP 服务器以支持 IPv6

学习如何编写 Java 程序来与启用 IPv6 的 FTP 服务器通信

developerWorks
文档选项

未显示需要 JavaScript 的文档选项

样例代码


最新推荐

Java 应用开发源动力 - 下载免费软件,快速启动开发


级别: 初级

Makham V. Kumar (makhamvk@in.ibm.com), 工程师, IBM

2006 年 8 月 07 日

下一代协议,Internet Protocol version 6 (IPv6),被广泛认为是 Internet 和网络世界的未来。这种认同已鼓励各个 IT 公司开发支持并使用 IPv6 地址格式进行相互通信的应用程序。在本文中,学习配置 File Transfer Protocol (FTP) 服务器以支持 IPv6,然后通过一个简单的使用 IPv6 地址的 Java 程序来与 FTP 服务器通信。

IPv6,又称下一代协议,是现有 IPv4 网络基础的超集。IPv6 与 IPv4 相兼容和协作,允许您升级您的 Internet 设备。有关 IPv6 的更多信息,请参考我的前一篇文章 “Discover Internet Protocol version 6 (IPv6)”,在 参考资料 部分可以找到这篇文章。

在本文中,我将向您演示如何在两种 FTP 服务器上配置 IPv6:Orenosv 1.0(与 Microsoft® Windows® 兼容)和 vsftpd 2.0.1-5(与 Linux® 兼容)。您将使用 IBM® Java™ Runtime Environment (JRE) 1.5.0 来编写和执行示例 Java 应用程序。

配置 Windows FTP 服务器 Orenosv 以支持 IPv6

您可以从 Orenosv 站点(参阅 参考资料)下载 Orenosv FTP 服务器。让我们来看看如何配置服务器来监听和接受 IPv6 地址。

首先,下载和安装服务器,默认安装在 $Drive/Program Files/Orenosv 中。创建用户帐户和其默认目录。转到安装位置,打开 passwd.txt,然后添加下面这一行:

ipv6:ipv6:/ftpusers/ipv6

在这一行中,ipv6 代表用户,ipv6 代表密码,而 ftpusers/ipv6 表示用户目录位置。图 1 显示了最终 passwd.txt 文件的样子。


图 1. 最终 passwd.txt 文件表示
最终 passwd.txt 文件表示

接着,在 $Install_Location/ftproot 中创建用户目录位置 ftpusers/ipv6。最终目录结构类似于:

$Install_Location\ftproot\ftpusers\ipv6

为您创建的 ipv6 用户提供访问控制。转到 $Install_Location,打开 acl.txt,然后添加下面这一行:

ftpuser/ipv6/* ALL="_all_" all="@admin"

最终 acl.txt 文件类似于 图 2


图 2. 最终 acl.txt 文件表示
最终 acl.txt 文件表示

接着,将 ipv6 用户添加到 admin 组。转到 $Install_Location,打开 grpdb.txt,然后将用户添加到 admin 组,类似于:

admin:admin1,admin2,ipv6

图 3 显示最终 grpdb.txt 文件的样子。


图 3. 最终 grpdb.txt 文件表示
最终 grpdb.txt 文件表示

要使 FTP 服务器接受 IPv6 请求,转到 $Install_Location,然后打开 http.conf。取消注释 http.conf 文件中的这些行:

ftp_enable = 1
ftp_listen = 0.0.0.0@21
ftp_port_srcport = 20

注释掉 http.conf 文件中的所有 Secure Sockets Layer (SSL) 条目,然后添加下面这一行以支持 IPv6:

ftp_listen=IPv6Address@21

IPv6Address 表示机器 IPv6 地址。这里是一个具有真实 IP 地址的示例:

ftp_listen = 2002:9b8:708a:0:0:0:0:1@21

要重新启动服务器,导航到 Start > Programs > Orenosv,并单击 Restart Orenosv Service。检查 FTP 服务器正在运行并可以监听 IPv6 请求。为此,打开命令行提示符并使用 IPv6 地址连接 FTP 服务器。如果登录成功,服务器看上去将与 图 4 类似。


图 4. 成功登录 FTP 服务器
登录 FTP 服务器

如果登录失败,请检查配置并再试一次。





回页首


配置 Linux FTP 服务器 vsftpd 以支持 IPv6

vsftpd 服务器默认附带有 Red Hat Enterprise Linux (RHEL)。让我们配置服务器来监听和接受 IPv6 地址。

首先,以 root 用户登录然后打开 vsftpd.conf,该文件通常位于 /etc/vsftpd 目录中。注释掉 vsftpd.conf 文件中的下面这一行:

listen=yes

将这一行添加到 vsftpd.conf 中以支持 IPv6:

listen_ipv6=yes

要重新启动 vsftpd 服务器,使用 service vsftpd restart 命令。如果重新启动失败,请检查 vsftpd.conf 条目。





回页首


编写示例 FTP 客户机程序

让我们编写一个简单的 Java FTP 客户机程序来实现下列功能:

  • 使用 IPv6 数字地址连接 FTP 服务器
  • 在与服务器建立连接之后显示用户登录目录下的文件
  • 提示用户输入要下载的文件名
  • 从 FTP 服务器下载各自的文件之后退出

您可以下载 zip 文件(参阅 下载),其中包含有用来运行程序的所有相关文件的完整代码;在 运行 Java 应用程序 一节参看更多有关说明。清单 1 解释程序的核心逻辑。


清单 1. 从支持 IPv6 的 FTP 服务器获取文件的示例 Java 程序
package com.ibm.ipv6.ftp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import sun.net.ftp.FtpClient;

/**
 * @author Makham Kumar
 *
 */
public class IPv6FTPClient extends FtpClient{

    private static final String sourceClass = "IPv6FTPClient";
    private static final String SPACE= " ";
    private static final String SEP = "-";
    private static final String LINE = "\n";

    public IPv6FTPClient(){ }

public static void main(String[] ipv6Main) {
      final String sourceMethod = "main";
      BufferedReader bRead = null;
      IPv6FTPClient ipv6Ins = null;
      String strOption = null;
      String strFileName = null;
      boolean bStatus = true;
	try {
   	      if(ipv6Main.length == 3)
   	      {
   	        ipv6Ins = new IPv6FTPClient();
   	        ipv6Ins.loginToMachine(ipv6Main[0], ipv6Main[1], ipv6Main[2]);
   	        ipv6Ins.listFileSets();
   	        // Capturing details from the user to download files from the FTP server

     	      do {
     	      console(LINE+LINE+SPACE+"Want to download a file ? [Y/N] : ");
              bRead = new BufferedReader(new InputStreamReader(System.in));
              strOption = bRead.readLine();
               if(strOption.equalsIgnoreCase("Y"))
               {
                console(LINE+SPACE+"Enter the file name to download : ");
                strFileName = bRead.readLine();
                bStatus = ipv6Ins.getFile(strFileName);
               }
               else if(strOption.equalsIgnoreCase("N"))
               {
                bStatus = false;
                ipv6Ins.closeServer();
                console(LINE+LINE+SPACE+"Closing the FTP server connection "+LINE);
               }
               else
               {
                bStatus = false;
                console(LINE+LINE+SPACE+"Entered invalid option.");
               }
              } while (bStatus);
               bRead.close();
          }
          else
          {
           console(LINE+"Entered invalid arguments");
          }
           console(LINE+LINE+SPACE+"Application existing !!!"+LINE+LINE);
         }catch(Exception eM)
          {
           eM.printStackTrace();
           console(LINE+"Application existing !!!"+LINE+LINE);
          }
         finally
         {
          try {
            if(bRead != null)
            bRead.close();
           } catch (Exception eFinally) {
            eFinally.printStackTrace();
           }
         }
 }


 /**
  * This utility method connects to FTP server using Username and Password
  * @return void
  */
 private void loginToMachine(String strMachine, String strUser, String strPasswd)
 {
    final String sourceMethod = "loginToMachine";
    try{
      openServer(strMachine);
      login(strUser,strPasswd);
      console(sourceMethod,"Successfully connected to FTP server");
      binary();
    } catch(Exception eLogin)
    {
    eLogin.printStackTrace();
    System.exit(0);
    }
 }


 /**
  * This utility method lists the names of the files in
  * the respective FTP user directory
  * @return void
  */
 private void listFileSets()
 {
    final String sourceMethod = "listFileSets";
    String fileName;
    BufferedReader reader = null;
    try{
        reader = new BufferedReader(new InputStreamReader(list()));
        while ((fileName = reader.readLine()) != null) {
        console(LINE+fileName);
            }
        reader.close();
       }catch(Exception eList)
       {
        eList.printStackTrace();
       }
       finally
       {
        try {
         if(reader != null)
         reader.close();
        }catch (Exception eFinally) {
         eFinally.printStackTrace();
       }
       }
 }


 /**
  * This utility method downloads the file from the FTP server to
  * the location where this Java program is executed
  * @return boolean
  */
 private boolean getFile(String fileName)
 {
    final String sourceMethod = "getFile";
    boolean bStatus = false;
    BufferedInputStream bInputStream = null; 
    FileOutputStream fOutputStream = null;
    String strPath = "."+File.separator+"download"+File.separator;
    try {
        bInputStream = new BufferedInputStream(get(fileName));
        fOutputStream = new FileOutputStream(strPath+fileName);
        int iStart = 0;
        byte[] byteArray = new byte[1024];

        while ((iStart = bInputStream.read(byteArray)) >= 0) {
           fOutputStream.write(byteArray, 0, iStart);
        }
        fOutputStream.close();
        bInputStream.close();
        console(sourceMethod,"Successfully downloaded the file : "+fileName);
        bStatus = true;
        } catch (Exception eGetFile) {
           eGetFile.printStackTrace();
        }
        finally
        {
		 try {
          if(fOutputStream != null)
           fOutputStream.close();
          if(bInputStream != null)
           bInputStream.close();
          } catch (Exception eFinally) {
           eFinally.printStackTrace();
          }
        }
    return bStatus;
  }


 /**
  * This utility method prints the text to console
  * @param String - Source method name
  * @param String - Message text
  * @return void
  */
 private static void console(String sourceMethod, String msgText)
 {
    System.out.println(LINE+SPACE+sourceClass+SEP+sourceMethod+SEP+msgText);
 }


 /**
  * This utility method prints the text to console
  * @param String - Message text
  * @return void
  */
 private static void console(String msgText)
 {
     System.out.print(msgText);
 }

}





回页首


运行 Java 应用程序

应用程序打包在一个压缩文件中。解压缩文件并下载到 SampleAppl 目录。lib 目录包含 Java Archive (JAR) 格式的应用程序,还有完整的 Java 源代码。来自 FTP 服务器的所有文件都下载到 download 目录。Run.bat 和 Run.sh 文件是特定于操作系统的脚本文件,用于运行应用程序。要运行应用程序,打开 Run.bat(对于 Windows)或 Run.sh(对于 Linux),并编辑这些登录细节的值:

set IPV6ADDRESS="$IPV6_ADDRESS"
set USERID="$USERID"
set PASSWD="$PASSWD"
set JAVA_HOME_15="$JAVA_HOME_15"

例如,您的登录细节可能类似于:

set IPV6ADDRESS="2002:9b8:708f:0:0:0:0:1"
set USERID="ipv6"
set PASSWD="ipv6"
set JAVA_HOME_15="C:\Makham\was\wasnd\java"		

打开命令行提示符,导航到脚本文件解压缩的目录,并执行 Run.bat 或 Run.sh,如 图 5 所示。


图 5. 应用程序输出信息
应用程序输出信息

在成功登录之后,应用程序列出文件并提示用户输入要下载的文件。本例使用 Welcome.txt。应用程序加载 Welcome.txt,将其放置在 download 目录,然后退出。

恭喜!您已成功配置 FTP 服务器以支持 IPv6,并使用 Java 应用程序从 FTP 服务器接收了文件。





回页首


结束语

您只学习了如何配置和编写 Java 程序来与启用 IPv6 的 FTP 服务器通信。您可以运用这些概念来编写可以与其他启用 IPv6 的服务器(如 Simple Mail Transfer Protocol (SMTP) 和 Post Office Protocol version 3 (POP3))通信的 Java 应用程序。






回页首


下载

描述名字大小下载方法
Sample code for this articlewa-ftpipv6-SampleAppl.zip6KBHTTP
关于下载方法的信息Get Adobe® Reader®




回页首


参考资料

学习

获得产品和技术
  • IBM 试用软件:改进您的下一个开发项目,可以通过从 developerWorks 直接下载获得这些软件。


讨论




回页首


关于作者

Makham V. Kumar 的照片

Makham V. Kumar 在 IBM 印度软件实验室的 IBM WebSphere Partner Gateway 开发团队工作。他的主要兴趣是使用 Java 2 Platform Enterprise Edition (J2EE) 中的新技术,业务集成领域,以及基于开放源码和 WebSphere 开发工具。Makham 是毕业于印度 Belgaum 卡纳塔克邦大学的一名工程师。

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::