Linux下用Sftp下载案例 -java

package com.kkyc.sass.common.utils;


import java.io.File;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class FtpUtils {
private static Logger logger = LoggerFactory.getLogger(FtpUtils.class);
private String host;
private int port;
private String username;
private String password;

/**
* @param host ip
* @param port 端口
* @param username 账号
* @param password 密码
* @return
* */
public FtpUtils(String host, int port, String username, String password) {
set(host, port, username, password);
}

public void set(String host, int port, String username, String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}

Session sshSession = null ;

/**
* 链接linux
* */
public ChannelSftp connect() {
ChannelSftp sftp = null ;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
logger.info(String.format("%s connect success" , host));
Channel channel = sshSession.openChannel("sftp");
channel.connect() ;
sftp = (ChannelSftp) channel;
} catch (Exception e) {
logger.error("connect:" + host , e );
close( null );
return null ;
}
return sftp;
}

/**
* linux远程下载
* @param file 待下载文件 /home/kksass/upload/***.zip
* @param backupDir 保存的目录 /data/kksass/backup
*/
public void get(File file, String backupDir) {
ChannelSftp sftp = connect() ;
try {
if(null != sftp){
// File file = new File( directory ) ;
// String parent = getParent( file ,2);
sftp.cd( "upload" );
/* File desc = new File(downloadFile);
FileOutputStream outputStream = new FileOutputStream(desc);
sftp.get(file.getName() , outputStream); */
// logger.info(file.getPath()+"---------"+downloadFile);
sftp.get(file.getName(), backupDir);
logger.info(String.format("down %s suc" , file.getPath()));
//outputStream.close() ;
}
} catch (Exception e) {
logger.error("down error :" + file.getPath() , e );
}finally{
close(sftp);
}
}

protected void close(ChannelSftp sftp){
if(sftp!=null){
sftp.disconnect() ;
sftp.exit();
}
if(sshSession!=null){
sshSession.disconnect();
}
}

/**
*
* @param desc
* @param systemType 1.Windows 2.Linux
* @return
*/
protected String getParent(File desc, Integer systemType){
if(systemType == 1){
String parent = desc.getParent();
return parent.replace("\\", "/");
}
return desc.getParent() ;
}

// 待调用方法
public static void ftpBackup(File file,String backupDir) {

/**
* @param host ip
* @param port 端口
* @param username 账号
* @param password 密码
* */
FtpUtils sftpUtils = new FtpUtils("120.23.69.217" , 22 , "user" , "password") ;

sftpUtils.get(file , backupDir);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值