需要导入jsch-0.1.52.jar

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector; import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException; /**
* 提供SFTP处理文件服务
*
* @author krm-hehongtao
* @date 2016-02-29
*
*/
public class SFTPUtil {
private JSch jSch = null;
private ChannelSftp sftp = null;// sftp主服务
private Channel channel = null;
private Session session = null; private String hostName = "192.168.0.177";// 远程服务器地址
private int port = 22;// 端口
private String userName = "weblogic";// 用户名
private String password = "weblogic";// 密码 public SFTPUtil(String hostName, int port, String userName, String password) {
this.hostName = hostName;
this.port = port;
this.userName = userName;
this.password = password;
} /**
* 连接登陆远程服务器
*
* @return
*/
public boolean connect() throws Exception {
try {
jSch = new JSch();
session = jSch.getSession(userName, hostName, port);
session.setPassword(password); session.setConfig(this.getSshConfig());
session.connect(); channel = session.openChannel("sftp");
channel.connect(); sftp = (ChannelSftp) channel;
System.out.println("登陆成功:" + sftp.getServerVersion()); } catch (JSchException e) {
System.err.println("SSH方式连接FTP服务器时有JSchException异常!");
System.err.println(e.getMessage());
throw e;
}
return true;
} /**
* 关闭连接
*
* @throws Exception
*/
private void disconnect() throws Exception {
try {
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
} catch (Exception e) {
throw e;
}
} /**
* 获取服务配置
*
* @return
*/
private Properties getSshConfig() throws Exception {
Properties sshConfig = null;
try {
sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no"); } catch (Exception e) {
throw e;
}
return sshConfig;
} /**
* 下载远程sftp服务器文件
*
* @param remotePath
* @param remoteFilename
* @param localFilename
* @return
*/
public boolean downloadFile(String remotePath, String remoteFilename, String localFilename)
throws SftpException, IOException, Exception {
FileOutputStream output = null;
boolean success = false;
try {
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
} File localFile = new File(localFilename);
// 有文件和下载文件重名
if (localFile.exists()) {
System.err.println("文件: " + localFilename + " 已经存在!");
return success;
}
output = new FileOutputStream(localFile);
sftp.get(remoteFilename, output);
success = true;
System.out.println("成功接收文件,本地路径:" + localFilename);
} catch (SftpException e) {
System.err.println("接收文件时有SftpException异常!");
System.err.println(e.getMessage());
return success;
} catch (IOException e) {
System.err.println("接收文件时有I/O异常!");
System.err.println(e.getMessage());
return success;
} finally {
try {
if (null != output) {
output.close();
}
// 关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
}
return success;
} /**
* 上传文件至远程sftp服务器
*
* @param remotePath
* @param remoteFilename
* @param localFileName
* @return
*/
public boolean uploadFile(String remotePath, String remoteFilename, String localFileName)
throws SftpException, Exception {
boolean success = false;
FileInputStream fis = null;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}
File localFile = new File(localFileName);
fis = new FileInputStream(localFile);
// 发送文件
sftp.put(fis, remoteFilename);
success = true;
System.out.println("成功发送文件,本地路径:" + localFileName);
} catch (SftpException e) {
System.err.println("发送文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
throw e;
} catch (Exception e) {
System.err.println("发送文件时有异常!");
System.err.println(e.getMessage());
throw e;
} finally {
try {
if (null != fis) {
fis.close();
}
// 关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
}
return success;
} /**
* 上传文件至远程sftp服务器
*
* @param remotePath
* @param remoteFilename
* @param input
* @return
*/
public boolean uploadFile(String remotePath, String remoteFilename, InputStream input)
throws SftpException, Exception {
boolean success = false;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
} // 发送文件
sftp.put(input, remoteFilename);
success = true;
} catch (SftpException e) {
System.err.println("发送文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
throw e;
} catch (Exception e) {
System.err.println("发送文件时有异常!");
System.err.println(e.getMessage());
throw e;
} finally {
try {
if (null != input) {
input.close();
}
// 关闭连接
disconnect();
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
} }
return success;
} /**
* 删除远程文件
*
* @param remotePath
* @param remoteFilename
* @return
* @throws Exception
*/
public boolean deleteFile(String remotePath, String remoteFilename) throws Exception {
boolean success = false;
try {
// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
} // 删除文件
sftp.rm(remoteFilename);
System.err.println("删除远程文件" + remoteFilename + "成功!");
success = true;
} catch (SftpException e) {
System.err.println("删除文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
return success;
} catch (Exception e) {
System.err.println("删除文件时有异常!");
System.err.println(e.getMessage());
return success;
} finally {
// 关闭连接
disconnect();
}
return success;
} /**
* 遍历远程文件
*
* @param remotePath
* @return
* @throws Exception
*/
public List<String> listFiles(String remotePath) throws SftpException {
List<String> ftpFileNameList = new ArrayList<String>();
Vector<LsEntry> sftpFile = sftp.ls(remotePath);
LsEntry isEntity = null;
String fileName = null;
Iterator<LsEntry> sftpFileNames = sftpFile.iterator();
while (sftpFileNames.hasNext()) {
isEntity = (LsEntry) sftpFileNames.next();
fileName = isEntity.getFilename();
System.out.println(fileName);
ftpFileNameList.add(fileName);
}
return ftpFileNameList;
} /**
* 判断路径是否存在
*
* @param remotePath
* @return
* @throws SftpException
*/
public boolean isExist(String remotePath) throws SftpException {
boolean flag = false;
try {
sftp.cd(remotePath);
System.out.println("存在路径:" + remotePath);
flag = true;
} catch (SftpException sException) { } catch (Exception Exception) {
}
return flag;
} public String getHostName() {
return hostName;
} public void setHostName(String hostName) {
this.hostName = hostName;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} /**
* 测试方法
*
*/
public static void main(String[] args) {
try {
SFTPUtil sftp = new SFTPUtil("192.168.0.177", 22, "weblogic", "weblogic");
System.out.println(new StringBuffer().append(" 服务器地址: ")
.append(sftp.getHostName()).append(" 端口:").append(sftp.getPort())
.append("用户名:").append(sftp.getUserName()).append("密码:")
.append(sftp.getPassword().toString()));
sftp.connect();
if (sftp.isExist("/home/weblogic/project")) {
sftp.listFiles("/home/weblogic/project");
sftp.downloadFile("/home/weblogic/project", "S123456_20150126.csv",
"D:\\S123456_20150126.csv");
// sftp.uploadFile("\\", "test.txt", "D:\\work\\readMe.txt");
// sftp.deleteFile("\\", "test.txt");
}
} catch (Exception e) {
System.out.println("异常信息:" + e.getMessage());
}
}
}

最新文章

  1. WPF自定义控件第二 - 转盘按钮控件
  2. MarkDown初体验
  3. 符号三角形——F
  4. Maven 和 Ant 的区别?
  5. Hello Indigo
  6. 老司机带你开飞机 一: mssql on linux 安装指导
  7. Jenkins具体安装与构建部署使用教程
  8. 解决 APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tas
  9. C++笔记004:C++类通俗点说
  10. Maven安装教程详解
  11. [Postman]拦截器扩展(15)
  12. centos7 开机启动服务链接说明
  13. django之ORM补充
  14. java笔记 -- java数据类型与类型转换
  15. ITxlab倡议启动“互联网X大脑”计划
  16. 使用requireJS加载不符合AMD规范的js文件:shim的使用方式和实现原理
  17. const 用法
  18. JS获取对象“属性和方法”的方法
  19. 22. Generate Parentheses(回溯)
  20. 安装GraphicsMagick

热门文章

  1. Arcgis api for js实现服务端地图的增删改查
  2. P1776 宝物筛选
  3. DataTable 转 JSON,XML转JSON
  4. Mysql5.7前后修改用户密码变化
  5. ajax工作原理/实例
  6. MySQL之高级操作
  7. 看完这一篇,再也不怕面试官问到IntentService的原理
  8. zookeeper代替eureka与springcloud整合
  9. PHP 数据库 ODBC创建 ODBC 连接
  10. Python decode()方法