sftp和ftp 根据配置远程服务器地址下载文件到当前服务
废话不多说,关键代码如下所示:
packagecom.eastrobot.remote;
importjava.util.List;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importcom.eastrobot.util.PropertiesUtil;
/**
*full.zhang
*
*ftp/sftp抽象方法类
*
*/
publicabstractclassFileRemote{
privatestaticfinalStringFTP_MODE="ftp";
privatestaticfinalStringSFTP_MODE="sftp";
publicstaticStringftproot;
publicstaticStringmode;
publicstaticStringhost;
publicstaticStringusername;
publicstaticStringpassword;
publicstaticStringport;
privatestaticFileRemoteclient=null;
//最大一次性下载50个文件
publicstaticintmax=50;
privatefinalstaticLogLOGGER=LogFactory.getLog(FileRemote.class);
publicstaticFileRemotegetInstance(){
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=newFileFtpRemote();
}elseif(mode.equals(SFTP_MODE)){
client=newFileSftpRemote();
}
}
returnclient;
}
/**
*执行定时任务
*/
publicvoidprocess(){
LOGGER.debug("----------------------------------------进入定时下载远程文件");
//创建线程池
ExecutorServiceexec=Executors.newSingleThreadExecutor();
exec.execute(newRunnable(){
@Override
publicvoidrun(){
//建立连接
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(Stringpath:listSourcePath){
downloadRemoteFile(path);
}
LOGGER.debug("____________________释放连接");
client.closeConnection();
}
});
exec.shutdown();
}
/**
*初始化连接
*
*@paramhost
*@paramport
*@paramusername
*@parampassword
*@throwsException
*@return
*/
publicabstractvoidinitFtpInfo(Stringhost,Stringport,Stringusername,Stringpassword);
/**
*下载远程服务下文件到本地服务
*
*@parampath
*@return
*@throwsException
*/
publicabstractvoiddownloadRemoteFile(StringfilePath);
/**
*获取远程服务下指定目录下的所有文件路径集合(包含子目录下文件)
*
*@parampath
*@return
*/
publicabstractList<String>listRemoteFilePath(Stringpath);
/**
*释放连接
*/
publicabstractvoidcloseConnection();
}
[java]viewplaincopy
packagecom.eastrobot.remote;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.commons.io.IOUtils;
importorg.apache.commons.lang.StringUtils;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
importcom.eastrobot.command.Commander;
publicclassFileFtpRemoteextendsFileRemote{
protectedFTPClientftpClient;
privateStringencoding="UTF-8";
privatebooleanbinaryTransfer=true;
privatefinalstaticLogLOGGER=LogFactory.getLog(FileFtpRemote.class);
@Override
publicvoidinitFtpInfo(Stringhost,Stringport,Stringusername,Stringpassword){
try{
//构造一个FtpClient实例
ftpClient=newFTPClient();
//设置字符集
ftpClient.setControlEncoding(encoding);
//连接FTP服务器
ftpClient.connect(host,StringUtils.isNotBlank(port)?Integer.valueOf(port):21);
//连接后检测返回码来校验连接是否成功
intreply=ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
//登陆到ftp服务器
if(ftpClient.login(username,password)){
setFileType();
}
ftpClient.login(username,password);
}else{
ftpClient.disconnect();
LOGGER.error("ftp服务拒绝连接!");
}
}catch(Exceptione){
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();//断开连接
}catch(IOExceptione1){
LOGGER.error("ftp服务连接断开失败!");
}
}
LOGGER.error("ftp服务连接失败!");
}
}
/**
*设置文件传输类型
*/
privatevoidsetFileType(){
try{
if(binaryTransfer){
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
}else{
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
@Override
publicvoiddownloadRemoteFile(StringfilePath){
if(StringUtils.endsWith(filePath,"/")||StringUtils.endsWith(filePath,File.separator)){
filePath=filePath.substring(0,filePath.length()-1);
}
FilesaveFile=newFile(filePath);
if(saveFile.exists()){
return;
}
//文件所在目录
Stringpath=filePath.substring(0,filePath.lastIndexOf("/"));
if(!StringUtils.endsWith(path,"/")&&!StringUtils.endsWith(path,File.separator)){
if(Commander.isLinux){
path=path+File.separator;
}else{
path=path+"/";
}
}
OutputStreamoutput=null;
try{
//创建目标文件路径
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
saveFile.createNewFile();
//转移到FTP服务器目录
ftpClient.changeWorkingDirectory(path);
output=newFileOutputStream(saveFile);
ftpClient.retrieveFile(filePath,output);
}catch(IOExceptione){
LOGGER.debug("文件:"+filePath+"______________________下载失败!");
e.printStackTrace();
}finally{
LOGGER.debug("文件:"+filePath+"______________________下载成功!");
IOUtils.closeQuietly(output);
}
}
@Override
publicList<String>listRemoteFilePath(Stringpath){
List<String>list=newArrayList<String>();
try{
if(!StringUtils.endsWith(path,"/")&&!StringUtils.endsWith(path,File.separator)){
if(Commander.isLinux){
path=path+File.separator;
}else{
path=path+"/";
}
}
booleanchangedir=ftpClient.changeWorkingDirectory(path);
if(changedir){
ftpClient.setControlEncoding(encoding);
FTPFile[]files=ftpClient.listFiles();
for(FTPFilefile: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()));
}elseif(changedir){
if(!StringUtils.endsWith(path,"/")&&!StringUtils.endsWith(path,File.separator)){
if(Commander.isLinux){
path=path+File.separator;
}else{
path=path+"/";
}
}
FilesaveFile=newFile(path+file.getName());
if(!saveFile.exists()){
list.add(path+file.getName());
}
}
}
}
}catch(Exceptione){
e.printStackTrace();
}
returnlist;
}
@Override
publicvoidcloseConnection(){
if(ftpClient!=null){
try{
ftpClient.logout();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
}
[java]viewplaincopy
packagecom.eastrobot.remote;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Properties;
importjava.util.Vector;
importorg.apache.commons.io.IOUtils;
importorg.apache.commons.lang.StringUtils;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importcom.eastrobot.command.Commander;
importcom.jcraft.jsch.Channel;
importcom.jcraft.jsch.ChannelSftp;
importcom.jcraft.jsch.JSch;
importcom.jcraft.jsch.Session;
importcom.jcraft.jsch.SftpATTRS;
importcom.jcraft.jsch.SftpException;
importcom.jcraft.jsch.ChannelSftp.LsEntry;
publicclassFileSftpRemoteextendsFileRemote{
protectedSessionsession=null;
protectedChannelSftpchannel=null;
privatefinalstaticLogLOGGER=LogFactory.getLog(FileSftpRemote.class);
@Override
publicvoidinitFtpInfo(Stringhost,Stringport,Stringusername,Stringpassword){
try{
JSchjsch=newJSch();//创建JSch对象
session=jsch.getSession(username,host,StringUtils.isNotBlank(port)?Integer.valueOf(port):22);
session.setPassword(password);//设置密码
Propertiesconfig=newProperties();
config.put("StrictHostKeyChecking","no");
session.setConfig(config);//为Session对象设置properties
session.setTimeout(60000);//设置timeout时间
session.connect();//通过Session建立链接
Channelchan=session.openChannel("sftp");//打开SFTP通道
chan.connect();//建立SFTP通道的连接
channel=(ChannelSftp)chan;
}catch(Exceptione){
LOGGER.error("sftp连接失败");
e.printStackTrace();
}
}
@Override
publicvoiddownloadRemoteFile(StringfilePath){
if(StringUtils.endsWith(filePath,"/")||StringUtils.endsWith(filePath,File.separator)){
filePath=filePath.substring(0,filePath.length()-1);
}
FilesaveFile=newFile(filePath);
FileOutputStreamoutput=null;
try{
if(saveFile.exists()){
return;
}
//创建目标文件路径
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
saveFile.createNewFile();
//文件所在目录
Stringpath=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,newFileOutputStream(saveFile));
LOGGER.debug("文件:"+filePath+"____________________________________________下载成功!");
}catch(Exceptione){
LOGGER.debug("文件:"+filePath+"____________________________________________下载失败!");
e.printStackTrace();
}finally{
IOUtils.closeQuietly(output);
}
}
@SuppressWarnings("unchecked")
@Override
publicList<String>listRemoteFilePath(Stringpath){
List<String>list=newArrayList<String>();
Vector<LsEntry>v=null;
try{
if(!StringUtils.endsWith(path,"/")&&StringUtils.endsWith(path,File.separator)){
path=path+File.separator;
}
v=channel.ls(path);
}catch(SftpExceptione){
e.printStackTrace();
}
for(LsEntrylsEntry:v){
if(list.size()>=max){
break;
}
if(!".".equals(lsEntry.getFilename())&&!"..".equals(lsEntry.getFilename())){
SftpATTRSattrs=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+"/";
}
}
FilesaveFile=newFile(path+lsEntry.getFilename());
if(!saveFile.exists()){
list.add(path+lsEntry.getFilename());
}
}
}
}
returnlist;
}
@Override
publicvoidcloseConnection(){
try{
if(channel!=null){
channel.quit();
channel.disconnect();
}
if(session!=null){
session.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
publicSessiongetSession(){
returnsession;
}
publicvoidsetSession(Sessionsession){
this.session=session;
}
publicChannelSftpgetChannel(){
returnchannel;
}
publicvoidsetChannel(ChannelSftpchannel){
this.channel=channel;
}
}
以上所述是小编给大家介绍的sftp和ftp根据配置远程服务器地址下载文件到当前服务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!