版本會持續更新,
當前版本:0.31
版本更新時間:2016-10-13
版本修正說明:
1.修正連接關閉,將關閉的方法改成私有,不允許使用者自行關閉(否則會導致連接池獲取錯誤)
2.優化刪除文件及文件夾,先判斷文件或文件夾是否存在,然後再刪除
前言:
sftp是Secure File Transfer Protocol的縮寫,安全文件傳送協議。可以爲傳輸文件提供一種安全的加密方法。sftp
與 ftp 有着幾乎一樣的語法和功能。SFTP 爲 SSH的一部分,是一種傳輸檔案至 Blogger 伺服器的安全方式。其實在SSH軟件包中,已經包含了一個叫作SFTP(Secure
File Transfer Protocol)的安全文件傳輸子系統,SFTP本身沒有單獨的守護進程,它必須使用sshd守護進程(端口號默認是22)來完成相應的連接操作,所以從某種意義上來說,SFTP並不像一個服務器程序,而更像是一個客戶端程序。SFTP同樣是使用加密傳輸認證信息和傳輸的數據,所以,使用SFTP是非常安全的。但是,由於這種傳輸方式使用了加密/解密技術,所以傳輸效率比普通的FTP要低得多,如果您對網絡安全性要求更高時,可以使用SFTP代替FTP。
本工具基於對JSch - Java Secure Channel進行的封裝,linux服務器不用安裝任何插件和軟件
使用指南:
首先是jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
Sftp協議工具類-v0.1
package com.noryar.filesystem.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
import com.jcraft.jsch.Channel;
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;
/**
* 文件工具類.
* @author Leon Lee
*/
public class SftpUtil {
/**
* 文件路徑前綴. /ddit-remote
*/
private static final String PRE_FIX = "/test-noryar";
/**
* 獲取sftp協議連接.
* @param host 主機名
* @param port 端口
* @param username 用戶名
* @param password 密碼
* @return 連接對象
* @throws JSchException 異常
*/
public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
final String password) throws JSchException {
ChannelSftp sftp = null;
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
return sftp;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件
* @throws Exception 異常
*/
public static File download(final String downloadFile, final String saveFile, final ChannelSftp sftp)
throws Exception {
FileOutputStream os = null;
File file = new File(saveFile);
try {
if (!file.exists()) {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
}
os = new FileOutputStream(file);
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
throw e;
} finally {
os.close();
}
return file;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件 byte[]
* @throws Exception 異常
*/
public static byte[] downloadAsByte(final String downloadFile, final ChannelSftp sftp) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
throw e;
} finally {
os.close();
}
return os.toByteArray();
}
/**
* 刪除文件-sftp協議.
* @param deleteFile 要刪除的文件
* @param sftp sftp連接
* @throws Exception 異常
*/
public static void rmFile(final String deleteFile, final ChannelSftp sftp) throws Exception {
try {
sftp.rm(deleteFile);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 刪除文件夾-sftp協議.
* @param deleteFile 文件夾路徑
* @param sftp sftp連接
* @throws Exception 異常
*/
public static void rmDir(final String pathString, final ChannelSftp sftp) throws Exception {
try {
sftp.rmdir(pathString);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件
* @param dir 保存路徑
* @param fileName 保存文件名
* @param sftp sftp連接
* @throws Exception 異常
*/
private static void uploadFile(final String srcFile, final String dir, final String fileName, final ChannelSftp sftp)
throws Exception {
mkdir(dir, sftp);
sftp.cd(dir);
sftp.put(srcFile, fileName);
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件路徑,/xxx/xxx.zip 或 x:/xxx/xxx.zip;
* @param sftp sftp連接
* @throws Exception 異常
*/
public static void uploadFile(final String srcFile, final ChannelSftp sftp) throws Exception {
try {
File file = new File(srcFile);
if (file.exists()) {
List<String> list = formatPath(srcFile);
uploadFile(srcFile, list.get(0), list.get(1), sftp);
}
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 根據路徑創建文件夾.
* @param dir 路徑 必須是 /xxx/xxx/xxx/ 不能就單獨一個/
* @param sftp sftp連接
* @throws Exception 異常
*/
public static boolean mkdir(final String dir, final ChannelSftp sftp) throws Exception {
try {
if (StringUtils.isBlank(dir))
return false;
String md = dir.replaceAll("\\\\", "/");
if (md.indexOf("/") != 0 || md.length() == 1)
return false;
return mkdirs(md, sftp);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 遞歸創建文件夾.
* @param dir 路徑
* @param sftp sftp連接
* @return 是否創建成功
* @throws SftpException 異常
*/
private static boolean mkdirs(final String dir, final ChannelSftp sftp) throws SftpException {
String dirs = dir.substring(1, dir.length() - 1);
String[] dirArr = dirs.split("/");
String base = "";
for (String d : dirArr) {
base += "/" + d;
if (dirExist(base + "/", sftp)) {
continue;
} else {
sftp.mkdir(base + "/");
}
}
return true;
}
/**
* 判斷文件夾是否存在.
* @param dir 文件夾路徑, /xxx/xxx/
* @param sftp sftp協議
* @return 是否存在
*/
public static boolean dirExist(final String dir, final ChannelSftp sftp) {
try {
Vector<?> vector = sftp.ls(dir);
if (null == vector)
return false;
else
return true;
} catch (SftpException e) {
return false;
}
}
/**
* 格式化路徑.
* @param srcPath 原路徑. /xxx/xxx/xxx.yyy 或 X:/xxx/xxx/xxx.yy
* @return list, 第一個是路徑(/xxx/xxx/),第二個是文件名(xxx.yy)
*/
public static List<String> formatPath(final String srcPath) {
List<String> list = new ArrayList<String>(2);
String dir = "";
String fileName = "";
String repSrc = srcPath.replaceAll("\\\\", "/");
int firstP = repSrc.indexOf("/");
int lastP = repSrc.lastIndexOf("/");
fileName = repSrc.substring(lastP + 1);
dir = repSrc.substring(firstP, lastP);
dir = PRE_FIX + (dir.length() == 1 ? dir : (dir + "/"));
list.add(dir);
list.add(fileName);
return list;
}
/**
* 關閉協議-sftp協議.
* @param sftp sftp連接
*/
public static void exit(final ChannelSftp sftp) {
sftp.exit();
}
public static void main(String[] args) throws Exception {
ChannelSftp sftp = getSftpConnect("192.168.0.35", 22, "root", "root");
String pathString = "C:\\test\\aaa\\Foxmail7.zip";
File file = new File(pathString);
System.out.println("上傳文件開始...");
uploadFile(pathString, sftp);
System.out.println("上傳成功,開始刪除本地文件...");
file.delete();
System.out.println("刪除完成,開始校驗本地文件...");
if (!file.exists()) {
System.out.println("文件不存在,開始從遠程服務器獲取...");
download(pathString, pathString, sftp);
System.out.println("下載完成");
} else {
System.out.println("在本地找到文件");
}
exit(sftp);
System.exit(0);
}
}
v0.2-新增sftp連接池
/**
* sftp連接池.
*/
private static final Map<String, Channel> SFTP_CHANNEL_POOL = new HashMap<String, Channel>();
/**
* 獲取sftp協議連接.
* @param host 主機名
* @param port 端口
* @param username 用戶名
* @param password 密碼
* @return 連接對象
* @throws JSchException 異常
*/
public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
final String password) throws JSchException {
Session sshSession = null;
Channel channel = null;
ChannelSftp sftp = null;
String key = host + "," + port + "," + username + "," + password;
if (null == SFTP_CHANNEL_POOL.get(key)) {
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();
channel = sshSession.openChannel("sftp");
channel.connect();
SFTP_CHANNEL_POOL.put(key, channel);
} else {
channel = SFTP_CHANNEL_POOL.get(key);
sshSession = channel.getSession();
if (!sshSession.isConnected())
sshSession.connect();
if (!channel.isConnected())
channel.connect();
}
sftp = (ChannelSftp) channel;
return sftp;
}
v0.3-修改了一些bug,新增遞歸刪除文件夾功能
package com.noryar.filesystem.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
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;
/**
* 文件工具類.<br>
* 1.所有的文件路徑必須以'/'開頭和結尾,否則路徑最後一部分會被當做是文件名<br>
* 2.方法出現異常的時候,會關閉sftp連接(但是不會關閉session和channel),異常會拋出
* @author Leon Lee
*/
public class SftpUtil {
/**
* 文件路徑前綴. /ddit-remote
*/
private static final String PRE_FIX = "/test-noryar";
/**
* sftp連接池.
*/
private static final Map<String, Channel> SFTP_CHANNEL_POOL = new HashMap<String, Channel>();
/**
* 獲取sftp協議連接.
* @param host 主機名
* @param port 端口
* @param username 用戶名
* @param password 密碼
* @return 連接對象
* @throws JSchException 異常
*/
public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
final String password) throws JSchException {
Session sshSession = null;
Channel channel = null;
ChannelSftp sftp = null;
String key = host + "," + port + "," + username + "," + password;
if (null == SFTP_CHANNEL_POOL.get(key)) {
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();
channel = sshSession.openChannel("sftp");
channel.connect();
SFTP_CHANNEL_POOL.put(key, channel);
} else {
channel = SFTP_CHANNEL_POOL.get(key);
sshSession = channel.getSession();
if (!sshSession.isConnected())
sshSession.connect();
if (!channel.isConnected())
channel.connect();
}
sftp = (ChannelSftp) channel;
return sftp;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件
* @throws Exception 異常
*/
public static File download(final String downloadFile, final String saveFile, final ChannelSftp sftp)
throws Exception {
FileOutputStream os = null;
File file = new File(saveFile);
try {
if (!file.exists()) {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
}
os = new FileOutputStream(file);
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
e.getMessage();
throw e;
} finally {
os.close();
}
return file;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件 byte[]
* @throws Exception 異常
*/
public static byte[] downloadAsByte(final String downloadFile, final ChannelSftp sftp) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
exit(sftp);
throw e;
} finally {
os.close();
}
return os.toByteArray();
}
/**
* 刪除文件-sftp協議.
* @param pathString 要刪除的文件
* @param sftp sftp連接
* @throws Exception 異常
*/
public static void rmFile(final String pathString, final ChannelSftp sftp) throws Exception {
try {
List<String> list = formatPath(pathString);
sftp.rm(list.get(0) + list.get(1));
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 刪除文件夾-sftp協議.如果文件夾有內容,則會拋出異常.
* @param pathString 文件夾路徑
* @param sftp sftp連接
* @param resursion 遞歸刪除
* @throws Exception 異常
*/
public static void rmDir(final String pathString, final ChannelSftp sftp, final boolean recursion) throws Exception {
try {
String fp = formatPath(pathString).get(0);
if (recursion)
exeRmRec(fp, sftp);
else
sftp.rmdir(fp);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 遞歸刪除執行.
* @param pathString 文件路徑
* @param sftp sftp連接
* @throws SftpException
*/
private static void exeRmRec(final String pathString, final ChannelSftp sftp) throws SftpException {
@SuppressWarnings("unchecked")
Vector<LsEntry> vector = sftp.ls(pathString);
if (vector.size() == 1) { // 文件,直接刪除
sftp.rm(pathString);
} else if (vector.size() == 2) { // 空文件夾,直接刪除
sftp.rmdir(pathString);
} else {
String fileName = "";
// 刪除文件夾下所有文件
for (LsEntry en : vector) {
fileName = en.getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
} else {
exeRmRec(pathString + "/" + fileName, sftp);
}
}
// 刪除文件夾
sftp.rmdir(pathString);
}
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件
* @param dir 保存路徑
* @param fileName 保存文件名
* @param sftp sftp連接
* @throws Exception 異常
*/
private static void uploadFile(final String srcFile, final String dir, final String fileName, final ChannelSftp sftp)
throws Exception {
mkdir(dir, sftp);
sftp.cd(dir);
sftp.put(srcFile, fileName);
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件路徑,/xxx/xx.yy 或 x:/xxx/xxx.yy
* @param sftp sftp連接
* @return 上傳成功與否
* @throws Exception 異常
*/
public static boolean uploadFile(final String srcFile, final ChannelSftp sftp) throws Exception {
try {
File file = new File(srcFile);
if (file.exists()) {
List<String> list = formatPath(srcFile);
uploadFile(srcFile, list.get(0), list.get(1), sftp);
return true;
}
return false;
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 根據路徑創建文件夾.
* @param dir 路徑 必須是 /xxx/xxx/ 不能就單獨一個/
* @param sftp sftp連接
* @throws Exception 異常
*/
public static boolean mkdir(final String dir, final ChannelSftp sftp) throws Exception {
try {
if (StringUtils.isBlank(dir))
return false;
String md = dir.replaceAll("\\\\", "/");
if (md.indexOf("/") != 0 || md.length() == 1)
return false;
return mkdirs(md, sftp);
} catch (Exception e) {
exit(sftp);
throw e;
}
}
/**
* 遞歸創建文件夾.
* @param dir 路徑
* @param sftp sftp連接
* @return 是否創建成功
* @throws SftpException 異常
*/
private static boolean mkdirs(final String dir, final ChannelSftp sftp) throws SftpException {
String dirs = dir.substring(1, dir.length() - 1);
String[] dirArr = dirs.split("/");
String base = "";
for (String d : dirArr) {
base += "/" + d;
if (dirExist(base + "/", sftp)) {
continue;
} else {
sftp.mkdir(base + "/");
}
}
return true;
}
/**
* 判斷文件夾是否存在.
* @param dir 文件夾路徑, /xxx/xxx/
* @param sftp sftp協議
* @return 是否存在
*/
private static boolean dirExist(final String dir, final ChannelSftp sftp) {
try {
Vector<?> vector = sftp.ls(dir);
if (null == vector)
return false;
else
return true;
} catch (SftpException e) {
return false;
}
}
/**
* 格式化路徑.
* @param srcPath 原路徑. /xxx/xxx/xxx.yyy 或 X:/xxx/xxx/xxx.yy
* @return list, 第一個是路徑(/xxx/xxx/),第二個是文件名(xxx.yy)
*/
public static List<String> formatPath(final String srcPath) {
List<String> list = new ArrayList<String>(2);
String repSrc = srcPath.replaceAll("\\\\", "/");
int firstP = repSrc.indexOf("/");
int lastP = repSrc.lastIndexOf("/");
String fileName = lastP + 1 == repSrc.length() ? "" : repSrc.substring(lastP + 1);
String dir = firstP == -1 ? "" : repSrc.substring(firstP, lastP);
dir = PRE_FIX + (dir.length() == 1 ? dir : (dir + "/"));
list.add(dir);
list.add(fileName);
return list;
}
/**
* 關閉協議-sftp協議.
* @param sftp sftp連接
*/
public static void exit(final ChannelSftp sftp) {
sftp.exit();
}
public static void main(String[] args) throws Exception {
ChannelSftp sftp = getSftpConnect("192.168.0.35", 22, "root", "root");
// String pathString = "C:\\test\\ccc\\Foxmail7.zip";
// File file = new File(pathString);
// System.out.println("上傳文件開始...");
// uploadFile(pathString, sftp);
// System.out.println("上傳成功,開始刪除本地文件...");
// file.delete();
// System.out.println("刪除完成,開始校驗本地文件...");
// if (!file.exists()) {
// System.out.println("文件不存在,開始從遠程服務器獲取...");
// download(pathString, pathString, sftp);
// System.out.println("下載完成");
// } else {
// System.out.println("在本地找到文件");
// }
rmDir("", sftp, true);
exit(sftp);
System.exit(0);
}
}
v0.31
package com.noryar.filesystem.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
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;
/**
* 文件工具類.<br>
* 1.所有的文件路徑必須以'/'開頭和結尾,否則路徑最後一部分會被當做是文件名<br>
* 2. @since version-0.3 方法出現異常的時候,<del>會關閉sftp連接(但是不會關閉session和channel)</del>(del @ version 0.31),異常會拋出<br>
* @author Leon Lee
*/
public class SftpUtil {
/**
* 文件路徑前綴. /ddit-remote
*/
private static final String PRE_FIX = "/test-noryar";
/**
* sftp連接池.
*/
private static final Map<String, Channel> SFTP_CHANNEL_POOL = new HashMap<String, Channel>();
/**
* 獲取sftp協議連接.
* @param host 主機名
* @param port 端口
* @param username 用戶名
* @param password 密碼
* @return 連接對象
* @throws JSchException 異常
*/
public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
final String password) throws JSchException {
Session sshSession = null;
Channel channel = null;
ChannelSftp sftp = null;
String key = host + "," + port + "," + username + "," + password;
if (null == SFTP_CHANNEL_POOL.get(key)) {
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();
channel = sshSession.openChannel("sftp");
channel.connect();
SFTP_CHANNEL_POOL.put(key, channel);
} else {
channel = SFTP_CHANNEL_POOL.get(key);
sshSession = channel.getSession();
if (!sshSession.isConnected())
sshSession.connect();
if (!channel.isConnected())
channel.connect();
}
sftp = (ChannelSftp) channel;
return sftp;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件
* @throws Exception 異常
*/
public static File download(final String downloadFile, final String saveFile, final ChannelSftp sftp)
throws Exception {
FileOutputStream os = null;
File file = new File(saveFile);
try {
if (!file.exists()) {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
}
os = new FileOutputStream(file);
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
throw e;
} finally {
os.close();
}
return file;
}
/**
* 下載文件-sftp協議.
* @param downloadFile 下載的文件
* @param saveFile 存在本地的路徑
* @param sftp sftp連接
* @return 文件 byte[]
* @throws Exception 異常
*/
public static byte[] downloadAsByte(final String downloadFile, final ChannelSftp sftp) throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
List<String> list = formatPath(downloadFile);
sftp.get(list.get(0) + list.get(1), os);
} catch (Exception e) {
throw e;
} finally {
os.close();
}
return os.toByteArray();
}
/**
* 刪除文件-sftp協議.
* @param pathString 要刪除的文件
* @param sftp sftp連接
* @throws SftpException 異常
*/
public static void rmFile(final String pathString, final ChannelSftp sftp) throws SftpException {
List<String> list = formatPath(pathString);
String dir = list.get(0);
String file = list.get(1);
if (dirExist(dir + file, sftp)) {
sftp.rm(list.get(0) + list.get(1));
}
}
/**
* 刪除文件夾-sftp協議.如果文件夾有內容,則會拋出異常.
* @param pathString 文件夾路徑
* @param sftp sftp連接
* @param resursion 遞歸刪除
* @throws SftpException 異常
*/
public static void rmDir(final String pathString, final ChannelSftp sftp, final boolean recursion)
throws SftpException {
String fp = formatPath(pathString).get(0);
if (dirExist(fp, sftp)) {
if (recursion)
exeRmRec(fp, sftp);
else
sftp.rmdir(fp);
}
}
/**
* 遞歸刪除執行.
* @param pathString 文件路徑
* @param sftp sftp連接
* @throws SftpException
*/
private static void exeRmRec(final String pathString, final ChannelSftp sftp) throws SftpException {
@SuppressWarnings("unchecked")
Vector<LsEntry> vector = sftp.ls(pathString);
if (vector.size() == 1) { // 文件,直接刪除
sftp.rm(pathString);
} else if (vector.size() == 2) { // 空文件夾,直接刪除
sftp.rmdir(pathString);
} else {
String fileName = "";
// 刪除文件夾下所有文件
for (LsEntry en : vector) {
fileName = en.getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
} else {
exeRmRec(pathString + "/" + fileName, sftp);
}
}
// 刪除文件夾
sftp.rmdir(pathString);
}
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件
* @param dir 保存路徑
* @param fileName 保存文件名
* @param sftp sftp連接
* @throws Exception 異常
*/
private static void uploadFile(final String srcFile, final String dir, final String fileName, final ChannelSftp sftp)
throws SftpException {
mkdir(dir, sftp);
sftp.cd(dir);
sftp.put(srcFile, fileName);
}
/**
* 上傳文件-sftp協議.
* @param srcFile 源文件路徑,/xxx/xx.yy 或 x:/xxx/xxx.yy
* @param sftp sftp連接
* @return 上傳成功與否
* @throws SftpException 異常
*/
public static boolean uploadFile(final String srcFile, final ChannelSftp sftp) throws SftpException {
File file = new File(srcFile);
if (file.exists()) {
List<String> list = formatPath(srcFile);
uploadFile(srcFile, list.get(0), list.get(1), sftp);
return true;
}
return false;
}
/**
* 根據路徑創建文件夾.
* @param dir 路徑 必須是 /xxx/xxx/ 不能就單獨一個/
* @param sftp sftp連接
* @throws SftpException 異常
*/
public static boolean mkdir(final String dir, final ChannelSftp sftp) throws SftpException {
if (StringUtils.isBlank(dir))
return false;
String md = dir.replaceAll("\\\\", "/");
if (md.indexOf("/") != 0 || md.length() == 1)
return false;
return mkdirs(md, sftp);
}
/**
* 遞歸創建文件夾.
* @param dir 路徑
* @param sftp sftp連接
* @return 是否創建成功
* @throws SftpException 異常
*/
private static boolean mkdirs(final String dir, final ChannelSftp sftp) throws SftpException {
String dirs = dir.substring(1, dir.length() - 1);
String[] dirArr = dirs.split("/");
String base = "";
for (String d : dirArr) {
base += "/" + d;
if (dirExist(base + "/", sftp)) {
continue;
} else {
sftp.mkdir(base + "/");
}
}
return true;
}
/**
* 判斷文件夾是否存在.
* @param dir 文件夾路徑, /xxx/xxx/
* @param sftp sftp協議
* @return 是否存在
*/
private static boolean dirExist(final String dir, final ChannelSftp sftp) {
try {
Vector<?> vector = sftp.ls(dir);
if (null == vector)
return false;
else
return true;
} catch (SftpException e) {
return false;
}
}
/**
* 格式化路徑.
* @param srcPath 原路徑. /xxx/xxx/xxx.yyy 或 X:/xxx/xxx/xxx.yy
* @return list, 第一個是路徑(/xxx/xxx/),第二個是文件名(xxx.yy)
*/
public static List<String> formatPath(final String srcPath) {
List<String> list = new ArrayList<String>(2);
String repSrc = srcPath.replaceAll("\\\\", "/");
int firstP = repSrc.indexOf("/");
int lastP = repSrc.lastIndexOf("/");
String fileName = lastP + 1 == repSrc.length() ? "" : repSrc.substring(lastP + 1);
String dir = firstP == -1 ? "" : repSrc.substring(firstP, lastP);
dir = PRE_FIX + (dir.length() == 1 ? dir : (dir + "/"));
list.add(dir);
list.add(fileName);
return list;
}
/**
* 關閉協議-sftp協議.(關閉會導致連接池異常,因此不建議用戶自定義關閉)
* @param sftp sftp連接
*/
private static void exit(final ChannelSftp sftp) {
sftp.exit();
}
public static void main(String[] args) throws Exception {
ChannelSftp sftp = getSftpConnect("192.168.0.35", 22, "root", "root");
// String pathString = "C:\\test\\ccc\\Foxmail7.zip";
// File file = new File(pathString);
// System.out.println("上傳文件開始...");
// uploadFile(pathString, sftp);
// System.out.println("上傳成功,開始刪除本地文件...");
// file.delete();
// System.out.println("刪除完成,開始校驗本地文件...");
// if (!file.exists()) {
// System.out.println("文件不存在,開始從遠程服務器獲取...");
// download(pathString, pathString, sftp);
// System.out.println("下載完成");
// } else {
// System.out.println("在本地找到文件");
// }
// rmDir("", sftp, true);
String path = "E:\\aaa.zip";
File file = SftpUtil.download(path, path, sftp);
// SftpUtil.exit(sftp);
exit(sftp);
System.exit(0);
}
}