FTP环境搭建及客户代码调用公共方法封装
一、背景
大型系统架构往往被分解为多个独立可运行的组件, 以满足性能、可靠性、可扩展性的需求。多个组件间的数据交互往往采用两种方式:小量数据通过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();
}
}
}
FTP环境搭建及客户代码调用公共方法封装的更多相关文章
- SFTP环境搭建及客户代码调用公共方法封装
一.背景 在开发应用软件的过程中,广泛使用FTP在各子系统间传送文本数据.但FTP存在安全问题,开放到外网存在安全漏洞,容易被攻击.替换方案是使用SFTP,SFTP提供更高的安全性,当然传输的效率也会 ...
- Angularjs调用公共方法与共享数据
这个问题场景是在使用ionic开发页面的过程中发现,多个页面对应的多个controller如何去调用公共方法,比如给ionic引入了toast插件,如何将这个插件的调用变成公共方法或者设置成工具类,因 ...
- python web自动化测试框架搭建(功能&接口)——接口公共方法
接口公共方法有:数据引擎.http引擎.Excel引擎 1.数据引擎:获取用例.结果检查.结果统计 # -*- coding:utf-8 -*- from XlsEngine import XlsEn ...
- [原创]LAMP+phpmyadmin+FTP环境搭建
***简单ftp服务器搭建: rpm –qa|grep vsftpd //检查是否安装服务 yum –y install vsftpd-* //安装服务 mkdir /var/ftp/uplo ...
- Angular中怎样创建service服务来实现组件之间调用公共方法
Angular组件之间不能互相调用方法,但是可以通过创建服务来实现公共方法的调用. 实现 创建服务命令 ng g service 服务路径/服务名 比如这里在app/services目录下创建stor ...
- JS常用公共方法封装
_ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...
- MPSOC之9——host、embeded间tftp、nfs、ftp环境搭建
tftp 可传输单个文件,不能传文件夹 需要通过命令传输文件,略显复杂 ==一般调试kernel时,用uboot通过tftp方式启动,不用每次都烧写存储介质== nfs 在host linux(ubu ...
- Centos7 ftp环境搭建
没玩过linux,折腾了半天的ftp,好不容易亲测通过了.不容易啊. 操作环境:vm虚拟机 centos7 首先:搞定网络问题:默认情况下使用ifconfig可以看到虚拟机下是无网络的.(注:虚拟机网 ...
- centos ftp服务器搭建 vsftpd 匿名访问配置方法 ftp 550 Failed to open file 错误处理
vsftpd是linux下常用的ftp服务软件,配置起来其实不复杂,只是网上很多文章,配置后都无法成功.我使用它是用于局域网内部分享文件的,所以使用匿名的方式. ftp本身密码是明文传输的,如果需要安 ...
随机推荐
- css设置兼容的透明样式
css设置透明并实现兼容: <style>div{ filter: alpha(opacity=80); -moz-opacity: 0.8; -khtml-opacity: 0.8; o ...
- 织梦使用if判断某个字段是否为空
织梦如何使用if判断某个字段是否为空呢?我们以文章页调用文章摘要为例: 使用if语句判断摘要是否为空,如果有摘要就显示摘要模块,如果没有就不显示 {dede:field.description run ...
- nxlog4go 按天或按文件大小分割日志
Building a new rotate file writer: rfw := l4g.NewRotateFileWriter("_rfw.log").SetMaxSize(1 ...
- HDU 5056
题意略. 巧妙的尺取法.我们来枚举每个字符str[i],计算以str[i]为结尾的符合题意的串有多少个.那么我们需要处理出str[i]的左边界j,在[j,i]之间的串均为符合题意的 串,那么str[i ...
- Shell 编程入门
首先创建一个文件: 在终端中输入如下命令: vi helloworld.sh 然后按i进行命令编写 下面这句话是必须写的 #!/bin/sh这句话是必须写的 #!/bin/sh a="hel ...
- 让网站通过Https访问
Prerequisites Before you begin, you should have some configuration already taken care of. We will be ...
- LeetCode第七天
==数组 Medium== 40.(162)Find Peak Element JAVA //斜率思想,二分法 class Solution { public int findPeakElement( ...
- hdu 2553 N皇后
这题要打表,不然超时. AC代码 #include<cstdio> #include<cstring> int n,cnt; int vis[3][20]; int ans[1 ...
- HDU - 1407 打表
思路:预处理10000以内所有数的三平方和即可. AC代码 #include <cstdio> #include <cmath> #include <algorithm& ...
- Spring 代理对象,cglib,jdk的问题思考,AOP 配置注解拦截 的一些问题.为什么不要注解在接口,以及抽象方法.
可以被继承 首先注解在类上是可以被继承的 在注解上用@Inherited /** * Created by laizhenwei on 17:49 2017-10-14 */ @Target({Ele ...