Java连接SFTP服务器

来源:互联网 发布:win7如何优化系统 编辑:程序博客网 时间:2024/06/07 05:58

需要的包可以直接在网上搜索打上就ok啦。


package com.sinosoft.common.ftp;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.sinosoft.client.GetPayService;


/**
 * SFTP工具类
 */
public class SFTPTool {


/**
* 读取配置文件连接sftp服务器
* @return
*/
public ChannelSftp getConnect() {

// 读取连接sftp的属性文件
String host = null;
String sOnlineSftpPort = null;
String username = null;
String password = null;
int port = 0;
try {
Properties p = new Properties();
p.load(GetPayService.class.getClassLoader().getResourceAsStream("sftp.properties"));
host = p.getProperty("onlineSftpServerIP");
sOnlineSftpPort = p.getProperty("onlineSftpPort");
username = p.getProperty("onlineSftpUserID");
password = p.getProperty("onlineSftpPassword");
System.out.println("SFTP:"+host);
System.out.println("SFTP:"+sOnlineSftpPort);
System.out.println("SFTP:"+username);
System.out.println("SFTP:"+password);
} catch (Exception e) {
System.out.println("SFTP:读取配置文件sftp.properties失败");
}
if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {
port = Integer.parseInt(sOnlineSftpPort);
}


ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("SFTP:Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("SFTP:Session connected.");
System.out.println("SFTP:Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("SFTP:Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}

// 直接更具IP地址链接服务器

public ChannelSftp getConnectIP(String host,String sOnlineSftpPort,String username,String password) {

// 读取连接sftp的属性文件
// String host = null;
// String sOnlineSftpPort = null;
// String username = null;
// String password = null;
int port = 0;
// try {
// Properties p = new Properties();
// p.load(GetPayService.class.getClassLoader().getResourceAsStream(filename));
// host = p.getProperty("onlineSftpServerIP");
// sOnlineSftpPort = p.getProperty("onlineSftpPort");
// username = p.getProperty("onlineSftpUserID");
// password = p.getProperty("onlineSftpPassword");
// System.out.println("SFTP:"+host);
// System.out.println("SFTP:"+sOnlineSftpPort);
// System.out.println("SFTP:"+username);
// System.out.println("SFTP:"+password);
// } catch (Exception e) {
// System.out.println("SFTP:读取配置文件sftp.properties失败");
// }
if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {
port = Integer.parseInt(sOnlineSftpPort);
}


ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("SFTP:Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("SFTP:Session connected.");
System.out.println("SFTP:Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("SFTP:Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}


/**
* SFTP文件上传
* @param directory
*            SFTP目录
* @param uploadFile
*            上传的文件
* @param sftp
*            SFTP连接对象
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* SFTP文件下载
* @param directory
*            SFTP目录
* @param downloadFile
*            文件名称
* @param saveFile
*            文件保存本地路径
* @param sftp
*            SFTP连接对象
*/
public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* SFTP文件删除

* @param directory
*            SFTP目录
* @param deleteFile
*            文件名称
* @param sftp
*            SFTP连接对象
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 获取文本信息

* @param fileUrl
*            文件存储位置
* @param sftp
*            SFTP连接对象
* @return
*/
public String getTextInfo(String fileUrl, ChannelSftp sftp) {
String info="";
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = sftp.get(fileUrl);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str + "\n");
}
info=buffer.toString();
return info;
} catch (Exception e) {
info="erro";
} finally {
if (inputStream != null && bufferedReader != null) {
try {
bufferedReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return info;
}


/**
* SFTP列出目录文件

* @param directory
*            SFTP目录
* @param sftp
*            SFTP连接对象
* @return 文件集合
* @throws SftpException
*/
public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException {
return sftp.ls(directory);
}


}
1 0