SFTPUtils工具类及使用
来源:互联网 发布:360手机数据恢复 编辑:程序博客网 时间:2024/06/08 09:50
配置maven
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version></dependency>
工具类
package com.hk.utils;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.JSchException;import com.jcraft.jsch.Session;import com.jcraft.jsch.SftpException;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.SocketException;import java.util.ArrayList;import java.util.List;import java.util.Properties;import java.util.Vector;/** * User: hk * Date: 2017/8/10 下午4:31 * version: 1.0 */public class SFTPUtil implements AutoCloseable { private Session session = null; private ChannelSftp channel = null; /** * 连接sftp服务器 * * @param serverIP 服务IP * @param port 端口 * @param userName 用户名 * @param password 密码 * @throws SocketException SocketException * @throws IOException IOException * @throws JSchException JSchException */ public void connectServer(String serverIP, int port, String userName, String password) throws SocketException, IOException, JSchException { JSch jsch = new JSch(); // 根据用户名,主机ip,端口获取一个Session对象 session = jsch.getSession(userName, serverIP, port); // 设置密码 session.setPassword(password); // 为Session对象设置properties Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // 通过Session建立链接 session.connect(); // 打开SFTP通道 channel = (ChannelSftp) session.openChannel("sftp"); // 建立SFTP通道的连接 channel.connect(); } /** * 自动关闭资源 */ public void close() { if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } } public List<ChannelSftp.LsEntry> getDirList(String path) throws SftpException { List<ChannelSftp.LsEntry> list = new ArrayList<>(); if (channel != null) { Vector vv = channel.ls(path); if (vv == null && vv.size() == 0) { return list; } else { Object[] aa = vv.toArray(); for (int i = 0; i < aa.length; i++) { ChannelSftp.LsEntry temp = (ChannelSftp.LsEntry) aa[i]; list.add(temp); } } } return list; } /** * 下载文件 * * @param remotePathFile 远程文件 * @param localPathFile 本地文件[绝对路径] * @throws SftpException SftpException * @throws IOException IOException */ public void downloadFile(String remotePathFile, String localPathFile) throws SftpException, IOException { try (FileOutputStream os = new FileOutputStream(new File(localPathFile))) { if (channel == null) throw new IOException("sftp server not login"); channel.get(remotePathFile, os); } } /** * 上传文件 * * @param remoteFile 远程文件 * @param localFile * @throws SftpException * @throws IOException */ public void uploadFile(String remoteFile, String localFile) throws SftpException, IOException { try (FileInputStream in = new FileInputStream(new File(localFile))) { if (channel == null) throw new IOException("sftp server not login"); channel.put(in, remoteFile); } }}
阅读全文
0 0
- SFTPUtils工具类及使用
- Banner 工具类及使用
- FTPUtil工具类及使用
- java国际化基础及工具类使用
- Jackson 工具类使用及配置指南
- 封装JSONUtils工具类及使用
- Jackson 工具类使用及配置指南
- Jackson 工具类使用及配置指南
- mogodb的使用及自制工具类
- Jackson 工具类使用及配置指南
- redis: jedis API使用及工具类
- OkHttp使用及工具类封装
- Jackson 工具类使用及配置指南
- ngwork 工具及使用
- 工具 | Procexp工具使用及案例说明
- IncrediBuild工具使用及设置
- 经典工具收藏及使用
- HttpWatch工具简介及使用
- 【欧拉计划 P4】Largest palindrome product 最大的回文数
- Java基础----字节流
- 十个JDBC的最佳实践
- CSU1215
- 读《程序员》2017年5期
- SFTPUtils工具类及使用
- Linux系统自己用到的一些基本命令总结(一)
- 用面向过程写一个五子棋
- easyui dialog属性
- jsonTree
- redis--内部结构--dict
- teaxtarea文本框剩余字数显示
- android studio如何开启与禁用版本控制VCS
- 【记忆化搜索】POJ