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
- Java连接SFTP服务器
- java连接sftp服务器 (随笔)
- Java连接SFTP服务器下载上传文件
- SFTP连接SAP HANA 服务器
- phpstorm sftp无法连接服务器
- java 通过sftp服务器上传下载删除文件
- sftp 文件上传 跨服务器 java
- Java从SFTP服务器下载文件
- java连接sftp服务+linux配置
- J2SSH Maverick 一段连接sftp服务器,上传,下载的代码:
- fileZilla错误:无法与 SFTP 服务器建立 FTP 连接
- 使用sublime text3 连接sftp/ftp(远程服务器)
- Sublime text3 连接sftp/ftp(远程服务器)
- 错误:无法与 SFTP 服务器建立 FTP 连接
- FTP服务器之间的文件传输(sftp连接)
- 错误:无法与 SFTP 服务器建立 FTP 连接
- 使用sublime text3 连接sftp/ftp(远程服务器)
- Sublime text3 连接sftp/ftp(远程服务器)
- 技术人员如何面试
- 数组去除重复
- eclipse启动无响应,停留在Loading workbench状态
- 弹性滑动导航菜单:
- Objective-C_语言_协议
- Java连接SFTP服务器
- learn python(1) print语法
- c语言心得
- YYYYMMDD int型时间转换
- python
- 我的课程总结
- 获取一周前日期
- Apache同一台主机配置多个站点
- 两个数组判断某一元素是否存在