用代码走进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) 文 ...
随机推荐
- perl学习笔记(一)
关于Perl: Perl是一种高级.通用.直译式.动态的程序语言.最初设计者拉里·沃尔(Larry Wall)为了让在UNIX上进行报表处理的工作变得更方便,决定开发一个通用的脚本语言,而在1987年 ...
- 基于HTML5 Canvas粒子效果文字动画特效
之前我们分享过很多超酷的文字特效,其中也有利用HTML5和CSS3的.今天我们要来分享一款基于HTML5 Canvas的文字特效,输入框中输入想要展示的文字,回车后即可在canvas上绘制出粒子效果的 ...
- spark sql 入门
package cn.my.sparksql import cn.my.sparkStream.LogLevel import org.apache.spark.{SparkConf, SparkCo ...
- MapReduce的集群行为和框架
MapReduce的集群行为 MapReduce的集群行为包括: 1.任务调度与执行MapReduce任务由一个JobTracker和多个TaskTracker两类节点控制完成.(1)JobTrack ...
- DRAM 各项基本参数记录
记录一下DRAM的各项基本参数 tCL CAS Latency CAS 潜伏期, 列地址寻找/读写命令执行完毕,准备要读出来,需要一个延时缓一缓 tRAS: RAS Active Timeing 行有 ...
- [uart]理解线路规程的作用
转自:http://biancheng.dnbcw.info/linux/336240.html Linux OS的设备驱动有相当经典的抽象思想以及分层思想.与通信世界里面的思想相一致. 一.在Lin ...
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
问题背景: 在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下: com.chenzhou.base.mybati ...
- linux网口驱动实现(待续)
驱动位于linux/driver/net下. 1. 寄存器读写 asm/io.h #define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile un ...
- git中文乱码解决方案
解决方案: 在bash提示符下输入: git config --global core.quotepath false core.quotepath设为false的话,就不会对0x80以上的字符进行q ...
- 【Qt Tips】QLineEdit内容过滤之setValidator和setInputMask浅析
1.QValidator分析 2.InputMask格式 3.测试代码和用例 项目路径: GitHub: https://github.com/Qunero/NeoQtTestDemo/tree/ma ...