Java使用sftp定时下载文件的示例代码
sftp简介
sftp是SecureFileTransferProtocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的网络的加密方法。sftp与ftp有着几乎一样的语法和功能。SFTP为SSH的其中一部分,是一种传输档案至Blogger伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(SecureFileTransferProtocol)的安全文件信息传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接和答复操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。
添加依赖
com.jcraft jsch 0.1.54
增加配置
sftp: ip:192.168.1.60 port:22 timeout:60000 retryTime:3 admin: username:admin password:2k3xrYjbd930.
代码示例
每天凌晨1点在多个用户目录中下载csv文件至本地tmp目录
@Service
publicclassSftpTaskextendsThread{
privateChannelSftpsftp;
privateSessionsession;
@Value("${sftp.admin.username}")
privateStringusername;
@Value("${sftp.admin.password}")
privateStringpassword;
@Value("${sftp.host}")
privateStringhost;
@Value("${sftp.port}")
privateIntegerport;
privateSftpServicesftpService;
publicEtlSftpTask(SftpServicesftpService){
this.sftpService=sftpService;
}
/**
*建立sftp连接
*/
privatevoidconnect(){
try{
JSchjSch=newJSch();
session=jSch.getSession(username,host,port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking","no");
session.connect();
Channelchannel=session.openChannel("sftp");
channel.connect();
sftp=(ChannelSftp)channel;
}catch(JSchExceptione){
e.printStackTrace();
}
}
/**
*关闭sftp连接
*/
publicvoidclose(){
try{
if(sftp!=null){
if(sftp.isConnected())sftp.disconnect();
}
if(session!=null){
if(session.isConnected())session.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*下载文件到本地
*
*@paramsource源文件
*@paramtarget目标文件
*@throwsSftpException异常
*@throwsFileNotFoundException异常
*/
privatevoiddownload(Stringsource,Stringtarget)throwsSftpException,FileNotFoundException{
sftp.get(source,newFileOutputStream(newFile(target)));
}
/**
*处理用户数据文件
*
*@paramroot数据文件根目录
*@paramlastTime上次处理文件的最后的时间
*@return本次处理文件的最后的时间
*/
privateIntegerhandle(Stringroot,IntegerlastTime){
Stringdirectory=root+"/event/";
Vectorfiles;
try{
files=sftp.ls(directory+"event_*.csv");
}catch(Exceptione){
e.printStackTrace();
return0;
}
//文件名
StringfileName;
//临时文件
StringtmpFile;
//文件更新时间
IntegermTime;
//文件最后更新时间
IntegermaxTime=lastTime;
//处理用户文件
for(Objecto:files){
try{
ChannelSftp.LsEntryf=(ChannelSftp.LsEntry)o;
//文件更新时间
mTime=f.getAttrs().getMTime();
if(mTime<=lastTime)continue;
//文件名
fileName=f.getFilename();
//最后处理事件
maxTime=Math.max(maxTime,mTime);
//下载文件
tmpFile="/tmp/"+fileName;
download(directory+fileName,tmpFile);
}catch(Exceptione){
//TODO错误日志
e.printStackTrace();
}
}
//返回文件最后的处理时间
returnmaxTime;
}
/**
*每天凌晨1点开始执行
*/
@Scheduled(cron="001***")
publicvoidtask(){
//获取sftp连接
connect();
Stringroot;
IntegerlastTime;
Longcid;
IntegermaxTime=lastTime;
//获取用户列表
for(SftpDTOsftpDTO:sftpService.findAll()){
//用户主目录
root=sftpDTO.getSftpRoot();
//上次处理文件的最后时间
lastTime=sftpDTO.getLastTime();
maxTime=Math.max(maxTime,handle(root,lastTime));
//更新最后处理时间
if(!maxTime.equals(lastTime)){
sftpDTO.setLastTime(maxTime);
sftpService.update(sftpDTO);
}
}
//释放sftp资源
close();
}
}
总结
以上所述是小编给大家介绍的Java使用sftp定时下载文件的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!