您的位置:首页 > 编程语言 > Java开发

java 代码链接SFTP,上传下载

2016-07-21 16:41 429 查看
依赖jar包:

<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>


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

import com.haokuaisheng.log.LogManager;
import com.haokuaisheng.sys.SystemUtils;
import com.haokuaisheng.sys.SystemUtils.SystemType;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SftpUtils {
protected String host;
protected int port;
protected String username;
protected String password;

/**
* @param host ip
* @param port 端口
* @param username 账号
* @param password 密码
* */
public SftpUtils(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();
LogManager.info(String.format("%s connect success" , host));
Channel channel = sshSession.openChannel("sftp");
channel.connect() ;
sftp = (ChannelSftp) channel;
} catch (Exception e) {
LogManager.err("connect:" + host , e );
close( null );
return null ;
}
return sftp;
}
/**
* linux上传文件
* */
public void upload(String directory,File file){
ChannelSftp sftp = connect() ;
try {
if(null != sftp){
sftp.cd(directory);
LogManager.info(String.format("cd %s" , directory));
sftp.put(new FileInputStream(file), file.getName());
}
} catch (Exception e) {
LogManager.err("upload:" + host , e );
}finally{
sftp.disconnect() ;
sftp.exit();
sshSession.disconnect();
}
}

/**
* linux下载文件
* */
public void get(String directory, String downloadFile) {
ChannelSftp sftp = connect() ;
try {
if(null != sftp){
File file = new File( directory ) ;
String parent = getParent( file );
sftp.cd( parent );
File desc = new File(downloadFile);
FileOutputStream outputStream = new FileOutputStream(desc);
sftp.get(file.getName() , outputStream);
LogManager.info(String.format("down %s suc" , directory));
outputStream.close() ;
}
} catch (Exception e) {
LogManager.err("down error :" + directory , e );
}finally{
close(sftp);
}
}

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

protected String getParent(File desc){
if(SystemUtils.getSystemType() == SystemType.WINDOWS){
String parent = desc.getParent();
return parent.replace("\\", "/");
}
return desc.getParent() ;
}

public static void main(String[] args) {

/**
* @param host ip
* @param port 端口
* @param username 账号
* @param password 密码
* */
SftpUtils sftpUtils = new SftpUtils("192.168.1.21" , 22 , "root" , "123456") ;
sftpUtils.upload("/opt/datas/uploads", new File("c://config.ini"));

sftpUtils.get("/opt/datas/uploads/config.ini" , "c://1.ini");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: