ftp 根据特定正则匹配文件名 下载到本地 并且上传文件到ftp java *** 最爱那水货
/**
* 建立FTP链接,FTP服务器地址、端口、登陆用户信息都在配置里配置即可。
* @throws IOException
*/
public boolean connectFtp(String ftpAddress, String ftpPort, String frpUserName, String frpPassword) throws IOException{
log.info("*****连接FTP服务器...*****");
try{
ftpClient.connect(ftpAddress, Integer.valueOf(ftpPort).intValue());
ftpClient.setControlEncoding("GB2312");
int reply = ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
if(ftpClient.login(frpUserName,frpPassword)){
log.info("*****连接FTP服务器成功!*****");
return true;
}
}else{
log.error("*****连接失败!响应代码为【"+ reply+"】*****");
}
disconnect();
}catch (Exception e) {
log.error("*****连接失败:" + e.getMessage());
}
return false;
} /**
* 设置FTP客户端 被动模式、数据模式为二进制、字符编码GBK
*/
public void setConnectType(){
try {
ftpClient.enterLocalPassiveMode();
ftpClient.setDefaultTimeout(1000 * 120);//120秒
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GB2312");
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 断开与远程服务器的连接
* @throws IOException
*/
public void disconnect() {
if(ftpClient.isConnected()){
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 过滤不符合的文件并批量下载
* @param remoteFileReg 文件前缀的正则表达式
* @param localPath 本地路径 .property 文件配置
* @param remote_down_path ftp文件路径
* @return List 下载到本地的文件路径 集合
* @throws IOException
*/
@SuppressWarnings("unchecked")
public List downloads(String remoteFileReg,String localPath,String remote_down_path) throws IOException{
List<String> fileNames = new ArrayList<String>();
log.info("*****转移到服务器目录:" + remote_down_path);
setConnectType();
boolean changeFlag = ftpClient.changeWorkingDirectory(remote_down_path);
FTPFile[] files = ftpClient.listFiles();
//String[] names = ftpClient.listNames();
log.info("*****改变目录是否成功:" + changeFlag);
log.info("*****服务器上report目录下所有校验报告的文件数为:【" +files.length + "】" );
if(files.length == 0){
log.info("*****未在服务器上找到文件!*****");
return null;
}else{//目录下有文件
//把 bak文件的前缀找出来 ,区分读取和未读取的xls 和 xlsx ,只下载 未读取的文件
List<String> bakList = new ArrayList<String>();
List<String> list = new ArrayList<String>(); for (int i = 0; i < files.length; i++) {
FTPFile ftpFile = files[i];
String fileName = ftpFile.getName(); if(!fileName.endsWith(".bak") && ftpFile == null){
log.info("******* "+ fileName + "文件无数据!");
continue;
}
//匹配指定的文件前缀 和后缀 为 .bak 格式的文件
//bak 文件是文件读取完毕后生成的标记文件
Pattern bak = Pattern.compile("^"+remoteFileReg+"\\.bak");
Matcher m = bak.matcher(fileName);
if (m.find()) {
//取.bak文件的 前缀
//System.out.println(fileName);
//System.out.println(fileName.split("\\.")[0]);
bakList.add(fileName.split("\\.")[0]);
continue;
} //匹配指定的文件前缀 和后缀 为 .xls .xlsx 格式的文件
//TODO 以后遇到其他的格式文件 需要把后缀抽出来作为参数传入
Pattern xls = Pattern.compile("^"+remoteFileReg+"\\.xls$"+"|"+"^"+remoteFileReg+"\\.xlsx$"+"|"+"^"+remoteFileReg+"\\.csv$");
Matcher mm = xls.matcher(fileName);
if(mm.find()){
list.add(fileName);
continue;
}
} Iterator<String> it = list.iterator();
while (it.hasNext()) {
String xls = it.next();
for (int i = 0; i < bakList.size(); i++) {
String bak = bakList.get(i);
//bak文件存在 , 去掉此文件
if (xls.indexOf(bak) !=-1) {
it.remove();
bakList.remove(i);
}
}
} for (String fFile : list) {
//下载未读取的文件
File downFile = new File(localPath + fFile);
//System.out.println(localPath);
File downPath = new File(localPath);
if(!downPath.exists()){
downPath.mkdirs();
}
String fileDir = remote_down_path + fFile;
OutputStream os = new FileOutputStream(downFile);
ftpClient.retrieveFile(new String(fileDir.getBytes("GB2312"),"ISO-8859-1"), os);
log.info("*****文件已下载到:" + downFile.getAbsolutePath() + "******");
fileNames.add(downFile.getAbsolutePath());
os.close();
}
log.info("**** 此次共下载了【"+list.size()+"】个文件! *****");
}
return fileNames;
} /**
* 上传标志文件
* @param remoteFile
* @param localFile
* @return
*/
public boolean upload(String localFileName,String remoteFileName){ boolean b = false;
try {
File file = new File(localFileName);
FileInputStream input = new FileInputStream(file);
b = ftpClient.changeWorkingDirectory(remoteFileName);
log.info("*****改变目录是否成功:" + b);
String remoteFile = remoteFileName + file.getName();
b = ftpClient.storeFile(new String(remoteFile.getBytes("GB2312"),"ISO-8859-1"), input);
if(b){
log.info(" ****** 标志文件"+localFileName+"上传成功!");
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
ftp 根据特定正则匹配文件名 下载到本地 并且上传文件到ftp java *** 最爱那水货的更多相关文章
- .Net 上传文件到ftp服务器和下载文件
突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...
- java 上传文件到 ftp 服务器
1. java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...
- java 上传文件到FTP(centos中的ftp服务)
ftp服务器系统:centos7 提供ftp的服务:vsftpd pom.xml 依赖 <dependency> <groupId>commons-net</groupI ...
- JAVA上传文件到FTP上
添加maven <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...
- Linux上F上传文件到FTP服务器
Linux上上传跟Windows上上传不一样,在Windows上测试没问题,但是放到Linux服务器上跑,上传的文件中文显示乱码.解决方案: FtpUtil.java红色标记处 package cn. ...
- Android上传文件之FTP
android客户端实现FTP文件(包括图片)上传应该没什么难度.写下来就了为了记录一下,望能帮到新手. 需要用到 commons-net-3.0.1.jar,后面附上jar包. 直接上代码: /** ...
- Android 上传文件到 FTP 服务器
实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...
- asp.net 服务器 上传文件到 FTP服务器
private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...
- 【python】用python脚本Paramiko实现远程执行命令、下载、推送/上传文件功能
Paramiko: paramiko模块,基于SSH用于连接远程服务器并执行相关操作. SSHClient: 用于连接远程服务器并执行基本命令 SFTPClient: 用于连接远程服务器并执行上传下载 ...
随机推荐
- Understanding RabbitMQ Exchange & Queue
Exchanges are the only places where messages could be published to; while queues are the only places ...
- pl/sql里的exists和in的差别
项目中有个需要需要如下pl/sql(数据库是MariaDB) ) AS small FROM cmp_ent_main a WHERE createTime<'2016-9-21' ,,) ) ...
- Java为何用xml做配置文件?
在Java世界里xml配置文件几乎是首选,xml有什么好的特性呢? xml能存储小量数据,仅仅是存储数据. xml可以跨平台,主流各种平台都对xml有支持, 真正的跨平台, xml读取速度快. xml ...
- 关于CefSharp的坎坷之路
项目背景: 公司的XX产品需要升级和以后支持多平台的使用.因为之前项目是由WPF实现的.目前以后想作为Html5来展示页面. 因为涉及到整体更改遇到的问题较多以及其他原因,所以只是内部内容区域先替换为 ...
- atom-shell程序打包
上一篇:http://www.cnblogs.com/luobenCode/p/4504910.html 打包之前要准备一下 请看这篇http://www.cnblogs.com/seanlv/arc ...
- Excel 导入到Datatable 中,再使用常规方法写入数据库
首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...
- 比较一下以“反射”和“表达式”执行方法的性能差异
由于频繁地使用反射会影响性能,所以ASP.NET MVC采用了表达式树的方式来执行目标Action方法.具体来说,ASP.NET MVC会构建一个表达式来体现针对目标Action方法的执行,并且将该表 ...
- Oracle层次查询
Oracle层次查询的语法如下: 下面根据两道“烧脑”的题具体来体现: 1. 根据时间先后顺序,十二星座的英文名称用逗号串起来为'Aries,Taurus,Gemini,Cancer,Leo,Virg ...
- 基于Metronic的Bootstrap开发框架经验总结(5)--Bootstrap文件上传插件File Input的使用
Bootstrap文件上传插件File Input是一个不错的文件上传控件,但是搜索使用到的案例不多,使用的时候,也是一步一个脚印一样摸着石头过河,这个控件在界面呈现上,叫我之前使用过的Uploadi ...
- 让你的Mac支持NTFS
前段时间换成Mac电脑之后,发现有一点不爽,不能在Mac下写入NTFS格式的磁盘,所以就去研究了一下. 解决方法有如下三种. 第一种,直接使用第三方软件,如Paragon NTFS for MAC,T ...