package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
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.Session;
public class SftpClientUtil {
private static Logger log = Logger.getLogger(SftpClientUtil.class);
/**
*
*
* 批量上传文件(文件夹)
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件夹
* @param sftp
* @since XMJR V2.0.2
*/
public static void batchUpload(String directory, String uploadFile, ChannelSftp sftp) {
File file = new File(uploadFile);
LinkedList<File> fList = new LinkedList<File>();
fList.addLast(file);
String str = null;
while (fList.size() > 0) {
File f = fList.removeFirst();
for (File file2 : f.listFiles()) {
if (file2.isFile()) {// 文件
System.err.println(file2);
str = file2.toString().replace("\\", "\\\\");
upload(directory, str, sftp);
}
}
}
}
/**
* 连接SFTP服务器工具类
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
* @since XMJR V2.0.2
*/
public static ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created!");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
// sshConfig.put("userauth.gssapi-with-mic", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected!");
System.out.println("Opening Channel!");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + "!!");
System.out.println("登录成功!!!");
} catch (Exception e) {
System.out.println("连接sftp失败!");
e.printStackTrace();
return null;
}
return sftp;
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
* @since XMJR V2.0.2
*/
public static void delete(String directory, String deleteFile, ChannelSftp sftp)
throws Exception {
try {
Vector fileList;
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (deleteFile.equals(fileName)) {
sftp.cd(directory);
sftp.rm(deleteFile);
log.error("delete: " + "删除成功!");
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("delete: " + e.getMessage());
throw new Exception("删除失败!");
}
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @param sftp
* @since XMJR V2.0.2
*/
public static boolean download(String directory, String downloadFile, String saveFile,
ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
System.err.println("下载成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 列出目录下的全部文件
*/
@SuppressWarnings("rawtypes")
public static List<String> listFiles(String directory, ChannelSftp sftp) throws Exception {
Vector fileList;
List<String> fileNameList = new ArrayList<String>();
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
}
fileNameList.add(fileName);
}
return fileNameList;
}
/**
* 重命名文件
*
* @param directory
* 要重命名文件所在目录
* @param oldName
* 要重命名的文件
* @param newName
* 新文件名
* @param sftp
* @since XMJR V2.0.2
*/
@SuppressWarnings("rawtypes")
public static boolean rename(String directory, String oldName, String newName, ChannelSftp sftp) {
try {
Vector fileList;
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (oldName.equals(fileName)) {
sftp.cd(directory);
sftp.rename(oldName, newName);
log.info("rename: 重命名文件成功!");
return true;
}
}
return false;
} catch (Exception e) {
e.printStackTrace();
log.error("rename: " + e.getMessage());
return false;
}
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
* @since XMJR V2.0.2
*/
public static boolean upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory); // cd()--进入指定目录
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
log.info("upload: " + "上传成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("upload: " + e.getMessage());
return false;
}
}
// /**
// *
// * 把文件压缩了再上传
// *
// * @param directory
// * 上传的目录
// * @param localPath
// * 要压缩的文件路径
// * @param zipPath
// * 压缩后保存的路径
// * @param sftp
// * @since XMJR V2.0.2
// */
//
// public static void zipFileAndUpload(String directory, String localPath,
// String zipPath,
// ChannelSftp sftp) {
// ZIPUtil.createZip(localPath, zipPath);
// //upload(directory, zipPath, sftp);
// }
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
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.Session;
public class SftpClientUtil {
private static Logger log = Logger.getLogger(SftpClientUtil.class);
/**
*
*
* 批量上传文件(文件夹)
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件夹
* @param sftp
* @since XMJR V2.0.2
*/
public static void batchUpload(String directory, String uploadFile, ChannelSftp sftp) {
File file = new File(uploadFile);
LinkedList<File> fList = new LinkedList<File>();
fList.addLast(file);
String str = null;
while (fList.size() > 0) {
File f = fList.removeFirst();
for (File file2 : f.listFiles()) {
if (file2.isFile()) {// 文件
System.err.println(file2);
str = file2.toString().replace("\\", "\\\\");
upload(directory, str, sftp);
}
}
}
}
/**
* 连接SFTP服务器工具类
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
* @since XMJR V2.0.2
*/
public static ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created!");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
// sshConfig.put("userauth.gssapi-with-mic", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected!");
System.out.println("Opening Channel!");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + "!!");
System.out.println("登录成功!!!");
} catch (Exception e) {
System.out.println("连接sftp失败!");
e.printStackTrace();
return null;
}
return sftp;
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
* @since XMJR V2.0.2
*/
public static void delete(String directory, String deleteFile, ChannelSftp sftp)
throws Exception {
try {
Vector fileList;
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (deleteFile.equals(fileName)) {
sftp.cd(directory);
sftp.rm(deleteFile);
log.error("delete: " + "删除成功!");
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("delete: " + e.getMessage());
throw new Exception("删除失败!");
}
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @param sftp
* @since XMJR V2.0.2
*/
public static boolean download(String directory, String downloadFile, String saveFile,
ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
System.err.println("下载成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 列出目录下的全部文件
*/
@SuppressWarnings("rawtypes")
public static List<String> listFiles(String directory, ChannelSftp sftp) throws Exception {
Vector fileList;
List<String> fileNameList = new ArrayList<String>();
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
}
fileNameList.add(fileName);
}
return fileNameList;
}
/**
* 重命名文件
*
* @param directory
* 要重命名文件所在目录
* @param oldName
* 要重命名的文件
* @param newName
* 新文件名
* @param sftp
* @since XMJR V2.0.2
*/
@SuppressWarnings("rawtypes")
public static boolean rename(String directory, String oldName, String newName, ChannelSftp sftp) {
try {
Vector fileList;
fileList = sftp.ls(directory);
Iterator it = fileList.iterator();
while (it.hasNext()) {
String fileName = ((LsEntry) it.next()).getFilename();
if (oldName.equals(fileName)) {
sftp.cd(directory);
sftp.rename(oldName, newName);
log.info("rename: 重命名文件成功!");
return true;
}
}
return false;
} catch (Exception e) {
e.printStackTrace();
log.error("rename: " + e.getMessage());
return false;
}
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
* @since XMJR V2.0.2
*/
public static boolean upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory); // cd()--进入指定目录
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
log.info("upload: " + "上传成功!");
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("upload: " + e.getMessage());
return false;
}
}
// /**
// *
// * 把文件压缩了再上传
// *
// * @param directory
// * 上传的目录
// * @param localPath
// * 要压缩的文件路径
// * @param zipPath
// * 压缩后保存的路径
// * @param sftp
// * @since XMJR V2.0.2
// */
//
// public static void zipFileAndUpload(String directory, String localPath,
// String zipPath,
// ChannelSftp sftp) {
// ZIPUtil.createZip(localPath, zipPath);
// //upload(directory, zipPath, sftp);
// }
}