import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class SftpFile {
static Session sshSession = null;
public static ChannelSftp getConnectIP(String host, String sOnlineSftpPort, String username, String password) {
int port = 0;
if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {
port = Integer.parseInt(sOnlineSftpPort);
}
ChannelSftp sftp = null;
try {
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 channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}
public static void upload(String directory, String uploadFile, ChannelSftp sftp) {
FileInputStream io = null;
try {
sftp.cd(directory);
File file = new File(uploadFile);
io = new FileInputStream(file);
sftp.put(io, file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != io) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
}
static boolean deleteDirFiles(String newsFile, ChannelSftp sftp) {
try {
sftp.cd(newsFile);
ListIterator a = sftp.ls(newsFile).listIterator();
while (a.hasNext()) {
LsEntry oj = (LsEntry) a.next();
SftpFile.delete(newsFile, oj.getFilename(), sftp);
}
} catch (Exception e) {
e.getMessage();
} finally {
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
return true;
}
public static void upload(String directory, String uploadFile, ChannelSftp sftp, String remoteFileName, boolean isRemote) {
FileInputStream io = null;
try {
boolean isExist = false;
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
isExist = true;
isExist = sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isExist = false;
}
}
if (!isExist) {
boolean existDir = SftpFile.isExistDir(directory, sftp);
if (!existDir) {
String pathArry[] = directory.split("/");
StringBuffer Path = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
Path.append(path + "/");
if (!SftpFile.isExistDir(Path + "", sftp)) {
sftp.mkdir(Path.toString());
}
sftp.cd(Path.toString());
}
}
}
sftp.cd(directory);
File file = new File(uploadFile);
io = new FileInputStream(file);
if (isRemote) {
sftp.put(io, remoteFileName);
} else {
sftp.put(io, file.getName());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != io) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
}
public static boolean isExistDir(String path, ChannelSftp sftp) {
boolean isExist = false;
try {
SftpATTRS sftpATTRS = sftp.lstat(path);
isExist = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isExist = false;
}
}
return isExist;
}
public static List<String> uploadZip(String directory, String uploadFile, ChannelSftp sftp, List<String> filePath) {
try {
List<String> list = new ArrayList<>();
boolean existDir = SftpFile.isExistDir(directory, sftp);
if (!existDir) {
sftp.mkdir(directory);
}
sftp.cd(directory);
int i = 1;
for (String newPath : filePath) {
FileInputStream io = null;
try {
File file = new File(uploadFile + newPath);
io = new FileInputStream(file);
sftp.put(io, newPath);
io.close();
list.add(newPath);
i++;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (null != io) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return list;
} catch (SftpException e) {
e.getMessage();
return null;
} finally {
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
}
public static 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();
} finally {
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
System.out.println("DOWNLOAD SUCCESS!");
}
public static List<String> check(String directory, ChannelSftp sftp) {
List<String> fileList = new ArrayList<>();
try {
sftp.cd(directory);
ListIterator a = sftp.ls(directory).listIterator();
while (a.hasNext()) {
LsEntry oj = (LsEntry) a.next();
System.out.println(oj.getFilename());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
return fileList;
}
public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
System.out.println("文件:"+deleteFile+" 删除成功!");
} catch (Exception e) {
e.printStackTrace();
}finally {
if (sftp.isConnected()) {
sshSession.disconnect();
sftp.disconnect();
}
}
}
public static void main(String[] args) {
ChannelSftp ftp = getConnectIP("10.0.0.131", "22", "root", "123456");
deleteDirFiles("/mydata/picture",ftp);
}
}