首页 技术 正文
技术 2022年11月15日
0 收藏 476 点赞 2,526 浏览 2720 个字

一、背景

大型系统架构往往被分解为多个独立可运行的组件, 以满足性能、可靠性、可扩展性的需求。多个组件间的数据交互往往采用两种方式:小量数据通过Sock函数、RMI、WebService等接口方式传递;大量采用文件方式传递。采用文件传递数据有两种方式:通过Windows的NFS系统,文件共享。采用FTP/SFTP做文件上传、下载。本文讲解采用FTP服务传递文件时,FTP服务器环境搭建及公共代码组件。

二、FTP Server环境搭建

2.1 下载开源组件Apache Server 1.0.6版本

到官方网站下载http://mina.apache.org/ftpserver-project

2.2 在users.properties配置文件添加用户名和密码

2.3 在ftpd-typical.xml设置端口,密码是否加密(本配置文件清除密码加密)

2.4 运行FTP Server

命令行执行:start “apache ftp server….” bin\ftpd.bat res\conf\ftpd-typical.xml

三、FTP客户端访问公共组件

3.1 添加commons-net-3.3.jar包到Eclipse.

3.2 公共基础类

import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;public class FTPUtility{    private String ftpIP = "";    private int ftpPort = 0;    private String userName = "";    private String passWord = "";    FTPClient client = null;    /**     * 构造函数,初始化连接FTP服务器的参数。     * @param ftpIP 服务器IP地址     * @param ftpPort 服务器端口     * @param userName 登陆用户名     * @param passWord 登陆密码     */    public FTPUtility(String ftpIP, int ftpPort, String userName, String passWord)    {        this.ftpIP = ftpIP;        this.ftpPort = ftpPort;        this.userName = userName;        this.passWord = passWord;    }    /**     * 连接FTP服务器。     * @return true: 连接成功; false:连接失败     */    public boolean connet()    {        // 1、连接FTP服务器        client = new FTPClient();        try        {            client.connect(ftpIP, ftpPort);            client.login(userName, passWord);            // 文件按二进制传输,按ASCII码传输EXCEL文件会被损坏。            client.setFileType(FTPClient.BINARY_FILE_TYPE);        }        catch (SocketException e)        {            e.printStackTrace();        }        catch (IOException e)        {            e.printStackTrace();        }        // 2、检验返回码,是否连接成功。        int replyCode = client.getReplyCode();        if (!FTPReply.isPositiveCompletion(replyCode))        {            try            {                client.disconnect();            }            catch (IOException e)            {                e.printStackTrace();            }            System.err.println("FTP server refused connection.");            return false;        }        return true;    }    /**     * 上传文件到FTP服务器     * @param localFilePath 待上传的本地文件完整路径     * @param ftpFileName 上传后保存到FTP服务器的名称(一般和本地文件名一致)     * @throws IOException IO异常     */    public void upLoadFile(String localFilePath, String ftpFileName) throws IOException    {        FileInputStream localIn = new FileInputStream(localFilePath);        client.storeFile(ftpFileName, localIn);        localIn.close();    }    /**     * 从FTP服务器下载文件到本地。     * @param ftpFileName 所下载文件在FTP服务器上的名称     * @param localFilePath 下载后文件保存的完整路径(文件名一般和FTP上保存的文件一致)     * @throws IOException IO异常     */    public void downLoadFile(String ftpFileName, String localFilePath) throws IOException    {        FileOutputStream localOut = new FileOutputStream(localFilePath);        client.retrieveFile(ftpFileName, localOut);        localOut.close();    }    /**     * 关闭FTP连接     * @throws IOException     */    public void disconnet() throws IOException    {        client.logout();    }}

四、客户端测试代码

public class TestMain{    public static void main(String[] args)    {        FTPUtility ftp = new FTPUtility("10.70.60.60", 2121, "admin", "admin");        if(!ftp.connet())        {            return;        }        try        {            ftp.upLoadFile("d:/temp/IBMS_NE_T.txt", "IBMS_NE_T.txt");            ftp.upLoadFile("d:/temp/2保修合同4.xlsx", "2保修合同4.xlsx");            ftp.downLoadFile("IBMS_NE_T.txt", "d:/temp/IBMS_NE_T_new.txt");            ftp.downLoadFile("2保修合同4.xlsx", "d:/temp/2保修合同4_new.xlsx");            ftp.disconnet();        }        catch (IOException e)        {            e.printStackTrace();        }    }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850