用代码走进Ftp
因为最近做一个关于集中采集的ftp改造开发。所以研究了哈ftp的开发。
一个简单常用的连接ftp的命令:ftp 主机ip
下面贴出我自己的ftp的demo。
1、FtpUtil工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
public class FtpUtil {
private FTPClient ftpClient;
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
/**
* 连接FTP
* @param ftpConfig
* @throws SocketException
* @throws IOException
*/
public void connectServer(FTPConfig ftpConfig) throws SocketException,
IOException {
String server = ftpConfig.getServer();
int port = ftpConfig.getPort();
String user = ftpConfig.getUsername();
String password = ftpConfig.getPassword();
String location = ftpConfig.getLocation();
connectServer(server, port, user, password, location);
}
/**
* 连接FTP
* @param server
* @param port
* @param user
* @param password
* @param path
* @throws SocketException
* @throws IOException
*/
public void connectServer(String server, int port, String user,
String password, String path) throws SocketException, IOException {
ftpClient = new FTPClient();
ftpClient.connect(server, port);
System.out.println("Connected to " + server + ".");
System.out.println(ftpClient.getReplyCode());
ftpClient.login(user, password);
// Path is the sub-path of the FTP path
if (path.length() != 0) {
ftpClient.changeWorkingDirectory(path);
}
}
/**
* FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
* 设置传输类型
* @param fileType
* @throws IOException
*/
public void setFileType(int fileType) throws IOException {
ftpClient.setFileType(fileType);
}
public void closeServer() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}
/**
* 改变目录
* @param path
* @return
* @throws IOException
*/
public boolean changeDirectory(String path) throws IOException {
return ftpClient.changeWorkingDirectory(path);
}
public boolean createDirectory(String pathName) throws IOException {
return ftpClient.makeDirectory(pathName);
}
public boolean removeDirectory(String path) throws IOException {
return ftpClient.removeDirectory(path);
}
/**
* 删除
* @param path
* @param isAll
* @return
* @throws IOException
*/
public boolean removeDirectory(String path, boolean isAll)
throws IOException {
if (!isAll) {
return removeDirectory(path);
}
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr == null || ftpFileArr.length == 0) {
return removeDirectory(path);
}
for (FTPFile ftpFile : ftpFileArr) {
String name = ftpFile.getName();
if (ftpFile.isDirectory()) {
System.out.println("* [sD]Delete subPath ["+path + "/" + name+"]");
removeDirectory(path + "/" + name, true);
} else if (ftpFile.isFile()) {
System.out.println("* [sF]Delete file ["+path + "/" + name+"]");
deleteFile(path + "/" + name);
} else if (ftpFile.isSymbolicLink()) {
} else if (ftpFile.isUnknown()) {
}
}
return ftpClient.removeDirectory(path);
}
/**
* 判断路径
* @param path
* @return
* @throws IOException
*/
public boolean existDirectory(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(path)) {
flag = true;
break;
}
}
return flag;
}
/**
* 获取所有文件
* @param path
* @return
* @throws IOException
*/
public List<String> getFileList(String path) throws IOException {
// listFiles return contains directory and file, it's FTPFile instance
// listNames() contains directory, so using following to filer directory.
//String[] fileNameArr = ftpClient.listNames(path);
FTPFile[] ftpFiles= ftpClient.listFiles(path);
List<String> retList = new ArrayList<String>();
if (ftpFiles == null || ftpFiles.length == 0) {
return retList;
}
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.isFile()) {
retList.add(ftpFile.getName());
}
}
return retList;
}
public boolean deleteFile(String pathName) throws IOException {
return ftpClient.deleteFile(pathName);
}
/**
* 上传文件
* @param fileName
* @param newName
* @return
* @throws IOException
*/
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
/**
* 上传文件
* @param fileName
* @return
* @throws IOException
*/
public boolean uploadFile(String fileName) throws IOException {
return uploadFile(fileName, fileName);
}
/**
* 上传文件
* @param iStream
* @param newName
* @return
* @throws IOException
*/
public boolean uploadFile(InputStream iStream, String newName)
throws IOException {
boolean flag = false;
try {
// can execute [OutputStream storeFileStream(String remote)]
// Above method return's value is the local file stream.
flag = ftpClient.storeFile(newName, iStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
/**
* 下载文件
* @param remoteFileName
* @param localFileName
* @return
* @throws IOException
*/
public boolean download(String remoteFileName, String localFileName)
throws IOException {
boolean flag = false;
File outfile = new File(localFileName);
OutputStream oStream = null;
try {
oStream = new FileOutputStream(outfile);
flag = ftpClient.retrieveFile(remoteFileName, oStream);
} catch (IOException e) {
flag = false;
return flag;
} finally {
oStream.close();
}
return flag;
}
/***
* 获取文件流
* @param sourceFileName
* @return
* @throws IOException
*/
public InputStream downFile(String sourceFileName) throws IOException {
return ftpClient.retrieveFileStream(sourceFileName);
}
}
用代码走进Ftp的更多相关文章
- 微信开发之SVN提交代码与FTP同步到apache的根目录
SVN是协同开发的,版本控制器,就是几个人同时开发,可以提交代码到SVN服务器,这样就可以协同开发,一般是早上上班首先更新下代码,然后自己修改代码 工作一天之后,修改代码之后,下班之前,更新代码,然后 ...
- 一句python代码搭建FTP服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib/ ...
- java代码实现ftp服务器的文件上传和下载
java代码实现文件上传到ftp服务器: 1:ftp服务器安装: 2:ftp服务器的配置: 启动成功: 2:客户端:代码实现文件的上传与下载: 1:依赖jar包: 2:sftpTools 工具类: ...
- python实战===一句python代码搭建FTP服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib ...
- 通过代码链接ftp上传下载删除文件
因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency> <groupId>com.jcraft</ ...
- java:nginx(java代码操作ftp服务器)
1.检查是否安装了vsftpd [root@linux01 ~]# rpm -qa|grep vsftpd 2.安装vsftpd [root@linux01 ~]# yum -y install vs ...
- Swift纯代码走进UICollectionView
Swift对于一门新的iOS编程语言,他的崛起是必然的 我们这群老程序员们学习新的技能也是必然的 不接受新技能将被这大群体无情的淘汰 So 我欣然接受这门看似不成熟的语言 下面我们说说Swift中比较 ...
- linux系统ftp命令
先来一段简单的ftp 下载脚本 ftp -i -n<<EOF open 14.2.33.211 user etl etl cd /etlfile/ftpfile lcd /etlfile/ ...
- FTP上传文件提示550错误原因分析。
今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...
随机推荐
- grails3.1.5 com.mysql.jdbc.Driver
[报错] Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1 ...
- Elasticsearch的javaAPI之get,delete,bulk
Elsasticsearch的javaAPI之get get API同意依据其id获得指定index中的基于json document.以下的样例得到一个JSON document(index为twi ...
- hbase中double类型数据做累加
public static Result incr(String tableFullName, String rowKey, String family, String qualifier, long ...
- C语言 · 陶陶摘苹果
算法提高 陶陶摘苹果 时间限制:1.0s 内存限制:256.0MB 问题描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出n个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个3 ...
- C语言 · 递归求二进制表示位数
算法训练 6-2递归求二进制表示位数 时间限制:10.0s 内存限制:256.0MB 问题描述 给定一个十进制整数,返回其对应的二进制数的位数.例如,输入十进制数9,其对应的二进制数 ...
- Eclipse 中 Debug 模式跳转到 exitCurrentThread 的问题解决
问题描述: Debug 模式启动项目,断点跳转到exitCurrentThread 解决方法: 修改Eclipse 配置 [window]->[Preferences]->[Java]-& ...
- 关于Cocos2d-x的粒子系统
1.cocos2d-x有一些自带的粒子效果,以后可以用到.当然,也可以自己定义一些粒子,不过要定义的话,虽然可以用cpp文件自己写,但是没有可视化的调节,还要设定各种奇怪的参数,是非常困难的.可以用一 ...
- 如何识别Java中的内存泄漏
Java开发人员都知道,Java利用垃圾回收机制来自动保持应用程序内存的干净和健康.然而可能有人不知道的是,即使使用了垃圾回收机制,Java中仍然可能存在内存泄漏风险.如果你碰到下面的错误代码: ja ...
- 下面属于javascript对象的有:( )
下面属于javascript对象的有:( ) A. Window B. Document C. Form D. String E. Navigator 解答:ACE
- 帝国cms的list.var中使用php函数
$r[title] = esub($r[title],8,'...'); //截取前8个字符,多出部分用...代替 $r[title] = str_replace("lhj",&q ...