sftp和ftp 根据配置远程服务器地址下载文件到当前服务

所属分类: 软件编程 / java 阅读数: 44
收藏 0 赞 0 分享

废话不多说,关键代码如下所示:

 

package com.eastrobot.remote; 
import java.util.List; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import com.eastrobot.util.PropertiesUtil; 
/** 
* full.zhang 
* 
* ftp/sftp抽象方法类 
* 
*/ 
public abstract class FileRemote { 
private static final String FTP_MODE = "ftp"; 
private static final String SFTP_MODE = "sftp"; 
public static String ftproot; 
public static String mode; 
public static String host; 
public static String username; 
public static String password; 
public static String port; 
private static FileRemote client = null; 
// 最大一次性下载50个文件 
public static int max = 50; 
private final static Log LOGGER = LogFactory.getLog(FileRemote.class); 
public static FileRemote getInstance() { 
if (client == null) { 
ftproot = PropertiesUtil.getString("transfer.root"); 
mode = PropertiesUtil.getString("transfer.mode"); 
host = PropertiesUtil.getString("transfer.host"); 
username = PropertiesUtil.getString("transfer.username"); 
password = PropertiesUtil.getString("transfer.password"); 
port = PropertiesUtil.getString("transfer.port"); 
if (mode.equals(FTP_MODE)) { 
client = new FileFtpRemote(); 
} else if (mode.equals(SFTP_MODE)) { 
client = new FileSftpRemote(); 
} 
} 
return client; 
} 
/** 
* 执行定时任务 
*/ 
public void process() { 
LOGGER.debug("----------------------------------------进入定时下载远程文件"); 
// 创建线程池 
ExecutorService exec = Executors.newSingleThreadExecutor(); 
exec.execute(new Runnable() { 
@Override 
public void run() { 
// 建立连接 
initFtpInfo(host, port, username, password); 
// 远程服务所有源文件路径集合 
List<String> listSourcePath = listRemoteFilePath(ftproot); 
if (listSourcePath.isEmpty()) { 
LOGGER.debug("____________________释放连接"); 
client.closeConnection(); 
return; 
} 
if (listSourcePath.size() > max) { 
listSourcePath = listSourcePath.subList(0, max); 
} 
for (String path : listSourcePath) { 
downloadRemoteFile(path); 
} 
LOGGER.debug("____________________释放连接"); 
client.closeConnection(); 
} 
}); 
exec.shutdown(); 
} 
/** 
* 初始化连接 
* 
* @param host 
* @param port 
* @param username 
* @param password 
* @throws Exception 
* @return 
*/ 
public abstract void initFtpInfo(String host, String port, String username, String password); 
/** 
* 下载远程服务下文件到本地服务 
* 
* @param path 
* @return 
* @throws Exception 
*/ 
public abstract void downloadRemoteFile(String filePath); 
/** 
* 获取远程服务下指定目录下的所有文件路径集合(包含子目录下文件) 
* 
* @param path 
* @return 
*/ 
public abstract List<String> listRemoteFilePath(String path); 
/** 
* 释放连接 
*/ 
public abstract void closeConnection(); 
} 
[java] view plain copy
package com.eastrobot.remote; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringUtils; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.FTPReply; 
import com.eastrobot.command.Commander; 
public class FileFtpRemote extends FileRemote { 
protected FTPClient ftpClient; 
private String encoding = "UTF-8"; 
private boolean binaryTransfer = true; 
private final static Log LOGGER = LogFactory.getLog(FileFtpRemote.class); 
@Override 
public void initFtpInfo(String host, String port, String username, String password) { 
try { 
// 构造一个FtpClient实例 
ftpClient = new FTPClient(); 
// 设置字符集 
ftpClient.setControlEncoding(encoding); 
// 连接FTP服务器 
ftpClient.connect(host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 21); 
// 连接后检测返回码来校验连接是否成功 
int reply = ftpClient.getReplyCode(); 
if (FTPReply.isPositiveCompletion(reply)) { 
// 登陆到ftp服务器 
if (ftpClient.login(username, password)) { 
setFileType(); 
} 
ftpClient.login(username, password); 
} else { 
ftpClient.disconnect(); 
LOGGER.error("ftp服务拒绝连接!"); 
} 
} catch (Exception e) { 
if (ftpClient.isConnected()) { 
try { 
ftpClient.disconnect(); // 断开连接 
} catch (IOException e1) { 
LOGGER.error("ftp服务连接断开失败!"); 
} 
} 
LOGGER.error("ftp服务连接失败!"); 
} 
} 
/** 
* 设置文件传输类型 
*/ 
private void setFileType() { 
try { 
if (binaryTransfer) { 
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
} else { 
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE); 
} 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
@Override 
public void downloadRemoteFile(String filePath) { 
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) { 
filePath = filePath.substring(0, filePath.length() - 1); 
} 
File saveFile = new File(filePath); 
if (saveFile.exists()) { 
return; 
} 
// 文件所在目录 
String path = filePath.substring(0, filePath.lastIndexOf("/")); 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
OutputStream output = null; 
try { 
// 创建目标文件路径 
if (!saveFile.getParentFile().exists()) { 
saveFile.getParentFile().mkdirs(); 
} 
saveFile.createNewFile(); 
// 转移到FTP服务器目录 
ftpClient.changeWorkingDirectory(path); 
output = new FileOutputStream(saveFile); 
ftpClient.retrieveFile(filePath, output); 
} catch (IOException e) { 
LOGGER.debug("文件:" + filePath + "______________________下载失败!"); 
e.printStackTrace(); 
} finally { 
LOGGER.debug("文件:" + filePath + "______________________下载成功!"); 
IOUtils.closeQuietly(output); 
} 
} 
@Override 
public List<String> listRemoteFilePath(String path) { 
List<String> list = new ArrayList<String>(); 
try { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
boolean changedir = ftpClient.changeWorkingDirectory(path); 
if (changedir) { 
ftpClient.setControlEncoding(encoding); 
FTPFile[] files = ftpClient.listFiles(); 
for (FTPFile file : files) { 
if (list.size() >= max) { 
break; 
} 
if (file.isDirectory()) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
list.addAll(this.listRemoteFilePath(path + file.getName())); 
} else if (changedir) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
File saveFile = new File(path + file.getName()); 
if (!saveFile.exists()) { 
list.add(path + file.getName()); 
} 
} 
} 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return list; 
} 
@Override 
public void closeConnection() { 
if (ftpClient != null) { 
try { 
ftpClient.logout(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
if (ftpClient.isConnected()) { 
try { 
ftpClient.disconnect(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 
} 
} 
} 
[java] view plain copy
package com.eastrobot.remote; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Properties; 
import java.util.Vector; 
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringUtils; 
import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import com.eastrobot.command.Commander; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.SftpATTRS; 
import com.jcraft.jsch.SftpException; 
import com.jcraft.jsch.ChannelSftp.LsEntry; 
public class FileSftpRemote extends FileRemote { 
protected Session session = null; 
protected ChannelSftp channel = null; 
private final static Log LOGGER = LogFactory.getLog(FileSftpRemote.class); 
@Override 
public void initFtpInfo(String host, String port, String username, String password) { 
try { 
JSch jsch = new JSch(); // 创建JSch对象 
session = jsch.getSession(username, host, StringUtils.isNotBlank(port) ? Integer.valueOf(port) : 22); 
session.setPassword(password); // 设置密码 
Properties config = new Properties(); 
config.put("StrictHostKeyChecking", "no"); 
session.setConfig(config); // 为Session对象设置properties 
session.setTimeout(60000); // 设置timeout时间 
session.connect(); // 通过Session建立链接 
Channel chan = session.openChannel("sftp"); // 打开SFTP通道 
chan.connect(); // 建立SFTP通道的连接 
channel = (ChannelSftp) chan; 
} catch (Exception e) { 
LOGGER.error("sftp连接失败"); 
e.printStackTrace(); 
} 
} 
@Override 
public void downloadRemoteFile(String filePath) { 
if (StringUtils.endsWith(filePath, "/") || StringUtils.endsWith(filePath, File.separator)) { 
filePath = filePath.substring(0, filePath.length() - 1); 
} 
File saveFile = new File(filePath); 
FileOutputStream output = null; 
try { 
if (saveFile.exists()) { 
return; 
} 
// 创建目标文件路径 
if (!saveFile.getParentFile().exists()) { 
saveFile.getParentFile().mkdirs(); 
} 
saveFile.createNewFile(); 
// 文件所在目录 
String path = filePath.substring(0, filePath.lastIndexOf("/")); 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
channel.cd(path); 
channel.get(filePath, new FileOutputStream(saveFile)); 
LOGGER.debug("文件:" + filePath + "____________________________________________下载成功!"); 
} catch (Exception e) { 
LOGGER.debug("文件:" + filePath + "____________________________________________下载失败!"); 
e.printStackTrace(); 
} finally { 
IOUtils.closeQuietly(output); 
} 
} 
@SuppressWarnings("unchecked") 
@Override 
public List<String> listRemoteFilePath(String path) { 
List<String> list = new ArrayList<String>(); 
Vector<LsEntry> v = null; 
try { 
if (!StringUtils.endsWith(path, "/") && StringUtils.endsWith(path, File.separator)) { 
path = path + File.separator; 
} 
v = channel.ls(path); 
} catch (SftpException e) { 
e.printStackTrace(); 
} 
for (LsEntry lsEntry : v) { 
if (list.size() >= max) { 
break; 
} 
if (!".".equals(lsEntry.getFilename()) && !"..".equals(lsEntry.getFilename())) { 
SftpATTRS attrs = lsEntry.getAttrs(); 
if (attrs.isDir()) { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
list.addAll(this.listRemoteFilePath(path + lsEntry.getFilename())); 
} else { 
if (!StringUtils.endsWith(path, "/") && !StringUtils.endsWith(path, File.separator)) { 
if (Commander.isLinux) { 
path = path + File.separator; 
} else { 
path = path + "/"; 
} 
} 
File saveFile = new File(path + lsEntry.getFilename()); 
if (!saveFile.exists()) { 
list.add(path + lsEntry.getFilename()); 
} 
} 
} 
} 
return list; 
} 
@Override 
public void closeConnection() { 
try { 
if (channel != null) { 
channel.quit(); 
channel.disconnect(); 
} 
if (session != null) { 
session.disconnect(); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
public Session getSession() { 
return session; 
} 
public void setSession(Session session) { 
this.session = session; 
} 
public ChannelSftp getChannel() { 
return channel; 
} 
public void setChannel(ChannelSftp channel) { 
this.channel = channel; 
} 
}

以上所述是小编给大家介绍的sftp和ftp 根据配置远程服务器地址下载文件到当前服务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多