2023-11-07 09:32:12 +08:00
|
|
|
|
package com.zcloud.util;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
2023-12-20 17:16:37 +08:00
|
|
|
|
import com.zcloud.dto.TenCorpDto;
|
|
|
|
|
import com.zcloud.entity.PageData;
|
|
|
|
|
import com.zcloud.service.mq.DockSendMessageService;
|
2023-12-18 16:14:27 +08:00
|
|
|
|
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
2023-12-20 17:16:37 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-11-08 17:15:45 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Configurable;
|
2023-11-08 10:24:18 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2023-11-08 17:15:45 +08:00
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2023-11-07 09:32:12 +08:00
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
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.SftpATTRS;
|
|
|
|
|
import com.jcraft.jsch.SftpException;
|
|
|
|
|
|
2023-12-18 16:14:27 +08:00
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
@Configuration
|
2023-11-07 09:32:12 +08:00
|
|
|
|
public class Smb {
|
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
|
|
|
|
|
public static String host;
|
|
|
|
|
public static Integer port;
|
|
|
|
|
public static String user;
|
|
|
|
|
public static String password;
|
|
|
|
|
public static String basePath;
|
2023-12-20 17:16:37 +08:00
|
|
|
|
public static String baseimgpath;
|
2023-11-08 17:15:45 +08:00
|
|
|
|
|
|
|
|
|
@Value("${smb.host}")
|
2023-12-18 16:14:27 +08:00
|
|
|
|
public void setHostPath(String hostProperties) {
|
2023-11-08 17:15:45 +08:00
|
|
|
|
host = hostProperties;
|
|
|
|
|
}
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
@Value("${smb.port}")
|
2023-12-18 16:14:27 +08:00
|
|
|
|
public void setPortPath(Integer portProperties) {
|
2023-11-08 17:15:45 +08:00
|
|
|
|
port = portProperties;
|
|
|
|
|
}
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
@Value("${smb.user}")
|
2023-12-18 16:14:27 +08:00
|
|
|
|
public void setUserPath(String userProperties) {
|
2023-11-08 17:15:45 +08:00
|
|
|
|
user = userProperties;
|
|
|
|
|
}
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-12-20 17:16:37 +08:00
|
|
|
|
@Value("${baseimgpath}")
|
|
|
|
|
public void setbaseimgpathProperties(String baseimgpathProperties) {
|
|
|
|
|
baseimgpath = baseimgpathProperties;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
@Value("${smb.password}")
|
2023-12-18 16:14:27 +08:00
|
|
|
|
public void setPasswordPath(String passwordProperties) {
|
2023-11-08 17:15:45 +08:00
|
|
|
|
password = passwordProperties;
|
|
|
|
|
}
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
@Value("${smb.basePath}")
|
2023-12-18 16:14:27 +08:00
|
|
|
|
public void setBasePath(String basePathProperties) {
|
2023-11-08 17:15:45 +08:00
|
|
|
|
basePath = basePathProperties;
|
|
|
|
|
}
|
2023-11-08 10:24:18 +08:00
|
|
|
|
|
2023-11-07 09:32:12 +08:00
|
|
|
|
// private static String host = "39.103.224.166";
|
|
|
|
|
//
|
|
|
|
|
// private static Integer port = 22;
|
|
|
|
|
//
|
|
|
|
|
// private static String user = "root";
|
|
|
|
|
//
|
|
|
|
|
// private static String password = "Zcloud@zcloud88888";
|
|
|
|
|
//
|
|
|
|
|
// private static String basePath = "/mnt/qgfile/file/";
|
2023-11-08 10:24:18 +08:00
|
|
|
|
// private static String host = "192.168.192.201";
|
|
|
|
|
//
|
|
|
|
|
// private static Integer port = Integer.valueOf(22);
|
|
|
|
|
//
|
|
|
|
|
// private static String user = "root";
|
|
|
|
|
//
|
|
|
|
|
// private static String password = "SJSKAQHBGLXT@20220311";
|
|
|
|
|
//
|
|
|
|
|
// private static String basePath = "/mnt/qask/file/";
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
public static final String SMBBAES_PATH = basePath;
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
public static void sshSftp(MultipartFile file, String fileName, String path) throws Exception {
|
|
|
|
|
Session session = null;
|
|
|
|
|
Channel channel = null;
|
|
|
|
|
JSch jsch = new JSch();
|
|
|
|
|
if (port <= 0) {
|
|
|
|
|
// 连接服务器,采用默认端口
|
|
|
|
|
session = jsch.getSession(user, host);
|
|
|
|
|
} else {
|
|
|
|
|
// 采用指定的端口连接服务器
|
|
|
|
|
session = jsch.getSession(user, host, port);
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
// 如果服务器连接不上,则抛出异常
|
|
|
|
|
if (session == null) {
|
|
|
|
|
throw new Exception("session is null");
|
|
|
|
|
}
|
|
|
|
|
// 设置登陆主机的密码
|
|
|
|
|
session.setPassword(password);
|
|
|
|
|
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
|
|
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
|
|
// 设置登陆超时时间
|
|
|
|
|
session.connect(30000);
|
|
|
|
|
OutputStream outstream = null;
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
try {
|
|
|
|
|
// 创建sftp通信通道
|
|
|
|
|
channel = (Channel) session.openChannel("sftp");
|
|
|
|
|
channel.connect(1000);
|
|
|
|
|
ChannelSftp sftp = (ChannelSftp) channel;
|
|
|
|
|
// 进入服务器指定的文件夹
|
2023-11-07 09:32:12 +08:00
|
|
|
|
// File dir = new File(basePath+"/"+path);
|
|
|
|
|
// dir.setWritable(true, false);
|
|
|
|
|
// if(!dir.exists()){
|
|
|
|
|
// dir.mkdirs();
|
|
|
|
|
// }
|
2023-11-08 17:15:45 +08:00
|
|
|
|
createDir(basePath + path, sftp);
|
|
|
|
|
sftp.cd(basePath + path);
|
|
|
|
|
// 列出服务器指定的文件列表
|
2023-11-07 09:32:12 +08:00
|
|
|
|
// Vector v = sftp.ls("*");
|
|
|
|
|
// for(int i=0;i<v.size();i++){
|
|
|
|
|
// System.out.println(v.get(i));
|
|
|
|
|
// }
|
2023-11-08 17:15:45 +08:00
|
|
|
|
// 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
sftp.put(file.getInputStream(), fileName);
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
// 关流操作
|
|
|
|
|
if (outstream != null) {
|
|
|
|
|
outstream.flush();
|
|
|
|
|
outstream.close();
|
|
|
|
|
}
|
|
|
|
|
if (session != null) {
|
|
|
|
|
session.disconnect();
|
|
|
|
|
}
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
channel.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-20 17:16:37 +08:00
|
|
|
|
|
2023-12-18 16:14:27 +08:00
|
|
|
|
/*
|
|
|
|
|
* bus_imgfiles表
|
|
|
|
|
* IMGFILES_ID,FOREIGN_KEY,TYPE
|
|
|
|
|
* 这三个值必须要传,所以用到Smb.sshSftp
|
|
|
|
|
* */
|
2023-12-20 17:16:37 +08:00
|
|
|
|
public static void sshSftp(MultipartFile file, String fileName, String path, String IMGFILES_ID, String FOREIGN_KEY, String TYPE) throws Exception {
|
2023-12-18 16:14:27 +08:00
|
|
|
|
Session session = null;
|
|
|
|
|
Channel channel = null;
|
|
|
|
|
JSch jsch = new JSch();
|
|
|
|
|
if (port <= 0) {
|
|
|
|
|
// 连接服务器,采用默认端口
|
|
|
|
|
session = jsch.getSession(user, host);
|
|
|
|
|
} else {
|
|
|
|
|
// 采用指定的端口连接服务器
|
|
|
|
|
session = jsch.getSession(user, host, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果服务器连接不上,则抛出异常
|
|
|
|
|
if (session == null) {
|
|
|
|
|
throw new Exception("session is null");
|
|
|
|
|
}
|
|
|
|
|
// 设置登陆主机的密码
|
|
|
|
|
session.setPassword(password);
|
|
|
|
|
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
|
|
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
|
|
// 设置登陆超时时间
|
|
|
|
|
session.connect(30000);
|
|
|
|
|
OutputStream outstream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 创建sftp通信通道
|
|
|
|
|
channel = (Channel) session.openChannel("sftp");
|
|
|
|
|
channel.connect(1000);
|
|
|
|
|
ChannelSftp sftp = (ChannelSftp) channel;
|
|
|
|
|
// 进入服务器指定的文件夹
|
|
|
|
|
// File dir = new File(basePath+"/"+path);
|
|
|
|
|
// dir.setWritable(true, false);
|
|
|
|
|
// if(!dir.exists()){
|
|
|
|
|
// dir.mkdirs();
|
|
|
|
|
// }
|
|
|
|
|
createDir(basePath + path, sftp);
|
|
|
|
|
sftp.cd(basePath + path);
|
|
|
|
|
// 列出服务器指定的文件列表
|
|
|
|
|
// Vector v = sftp.ls("*");
|
|
|
|
|
// for(int i=0;i<v.size();i++){
|
|
|
|
|
// System.out.println(v.get(i));
|
|
|
|
|
// }
|
|
|
|
|
// 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
|
2023-12-20 17:16:37 +08:00
|
|
|
|
DockSendMessageService sendMessageService = SpringUtil.getBean(DockSendMessageService.class);
|
2023-12-18 16:14:27 +08:00
|
|
|
|
sftp.put(file.getInputStream(), fileName);
|
2023-12-20 17:16:37 +08:00
|
|
|
|
PageData pageData = new PageData();
|
|
|
|
|
pageData.put("IMGFILES_ID", IMGFILES_ID);
|
|
|
|
|
pageData.put("FOREIGN_KEY", FOREIGN_KEY);
|
|
|
|
|
pageData.put("TYPE", TYPE);
|
|
|
|
|
pageData.put("BASEIMGPATH", baseimgpath);
|
|
|
|
|
pageData.put("FILEPATH", path);
|
|
|
|
|
pageData.put("FILENAME", fileName);
|
|
|
|
|
TenCorpDto tenCorpDto = new TenCorpDto();
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-12-20 17:16:37 +08:00
|
|
|
|
tenCorpDto.setMessage("图片消息");
|
|
|
|
|
tenCorpDto.setData(pageData);
|
|
|
|
|
tenCorpDto.setId(UuidUtil.get32UUID());
|
|
|
|
|
tenCorpDto.setTopic("dockingPicture");
|
|
|
|
|
tenCorpDto.setProducer_name("qa-prevention-czks");
|
|
|
|
|
sendMessageService.sendMessagePicture(tenCorpDto);
|
2023-12-18 16:14:27 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
// 关流操作
|
|
|
|
|
if (outstream != null) {
|
|
|
|
|
outstream.flush();
|
|
|
|
|
outstream.close();
|
|
|
|
|
}
|
|
|
|
|
if (session != null) {
|
|
|
|
|
session.disconnect();
|
|
|
|
|
}
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
channel.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-20 17:16:37 +08:00
|
|
|
|
|
2023-12-18 16:14:27 +08:00
|
|
|
|
/*
|
|
|
|
|
* bus_imgfiles表
|
|
|
|
|
* IMGFILES_ID,FOREIGN_KEY,TYPE
|
|
|
|
|
* 这三个值必须要传,所以用到Smb.sshSftp
|
|
|
|
|
* */
|
2023-12-20 17:16:37 +08:00
|
|
|
|
public static void sshSftpForInput(InputStream fileI, String fileName, String path, String IMGFILES_ID, String FOREIGN_KEY, String TYPE) throws Exception {
|
2023-12-18 16:14:27 +08:00
|
|
|
|
Session session = null;
|
|
|
|
|
Channel channel = null;
|
|
|
|
|
JSch jsch = new JSch();
|
|
|
|
|
if (port <= 0) {
|
|
|
|
|
// 连接服务器,采用默认端口
|
|
|
|
|
session = jsch.getSession(user, host);
|
|
|
|
|
} else {
|
|
|
|
|
// 采用指定的端口连接服务器
|
|
|
|
|
session = jsch.getSession(user, host, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果服务器连接不上,则抛出异常
|
|
|
|
|
if (session == null) {
|
|
|
|
|
throw new Exception("session is null");
|
|
|
|
|
}
|
|
|
|
|
// 设置登陆主机的密码
|
|
|
|
|
session.setPassword(password);
|
|
|
|
|
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
|
|
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
|
|
// 设置登陆超时时间
|
|
|
|
|
session.connect(30000);
|
|
|
|
|
OutputStream outstream = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 创建sftp通信通道
|
|
|
|
|
channel = (Channel) session.openChannel("sftp");
|
|
|
|
|
channel.connect(1000);
|
|
|
|
|
ChannelSftp sftp = (ChannelSftp) channel;
|
|
|
|
|
// 进入服务器指定的文件夹
|
|
|
|
|
// File dir = new File(basePath+"/"+path);
|
|
|
|
|
// dir.setWritable(true, false);
|
|
|
|
|
// if(!dir.exists()){
|
|
|
|
|
// dir.mkdirs();
|
|
|
|
|
// }
|
|
|
|
|
createDir(basePath + path, sftp);
|
|
|
|
|
sftp.cd(basePath + path);
|
|
|
|
|
// 列出服务器指定的文件列表
|
|
|
|
|
// Vector v = sftp.ls("*");
|
|
|
|
|
// for(int i=0;i<v.size();i++){
|
|
|
|
|
// System.out.println(v.get(i));
|
|
|
|
|
// }
|
|
|
|
|
// 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
|
|
|
|
|
|
|
|
|
|
sftp.put(fileI, fileName);
|
2023-12-20 17:16:37 +08:00
|
|
|
|
DockSendMessageService sendMessageService = SpringUtil.getBean(DockSendMessageService.class);
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-12-20 17:16:37 +08:00
|
|
|
|
PageData pageData = new PageData();
|
|
|
|
|
pageData.put("IMGFILES_ID", IMGFILES_ID);
|
|
|
|
|
pageData.put("FOREIGN_KEY", FOREIGN_KEY);
|
|
|
|
|
pageData.put("TYPE", TYPE);
|
|
|
|
|
pageData.put("FILEPATCH", path);
|
|
|
|
|
TenCorpDto tenCorpDto = new TenCorpDto();
|
|
|
|
|
tenCorpDto.setMessage("文件消息");
|
|
|
|
|
tenCorpDto.setData(pageData);
|
|
|
|
|
tenCorpDto.setId(UuidUtil.get32UUID());
|
|
|
|
|
tenCorpDto.setTopic("docking");
|
|
|
|
|
tenCorpDto.setProducer_name("qa-prevention-czks");
|
|
|
|
|
sendMessageService.sendMessagePicture(tenCorpDto);
|
2023-12-18 16:14:27 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
// 关流操作
|
|
|
|
|
if (outstream != null) {
|
|
|
|
|
outstream.flush();
|
|
|
|
|
outstream.close();
|
|
|
|
|
}
|
|
|
|
|
if (session != null) {
|
|
|
|
|
session.disconnect();
|
|
|
|
|
}
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
channel.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
public static void sshSftpForInput(InputStream fileI, String fileName, String path) throws Exception {
|
|
|
|
|
Session session = null;
|
|
|
|
|
Channel channel = null;
|
|
|
|
|
JSch jsch = new JSch();
|
|
|
|
|
if (port <= 0) {
|
|
|
|
|
// 连接服务器,采用默认端口
|
|
|
|
|
session = jsch.getSession(user, host);
|
|
|
|
|
} else {
|
|
|
|
|
// 采用指定的端口连接服务器
|
|
|
|
|
session = jsch.getSession(user, host, port);
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
// 如果服务器连接不上,则抛出异常
|
|
|
|
|
if (session == null) {
|
|
|
|
|
throw new Exception("session is null");
|
|
|
|
|
}
|
|
|
|
|
// 设置登陆主机的密码
|
|
|
|
|
session.setPassword(password);
|
|
|
|
|
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
|
|
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
|
|
// 设置登陆超时时间
|
|
|
|
|
session.connect(30000);
|
|
|
|
|
OutputStream outstream = null;
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
try {
|
|
|
|
|
// 创建sftp通信通道
|
|
|
|
|
channel = (Channel) session.openChannel("sftp");
|
|
|
|
|
channel.connect(1000);
|
|
|
|
|
ChannelSftp sftp = (ChannelSftp) channel;
|
|
|
|
|
// 进入服务器指定的文件夹
|
2023-11-07 09:32:12 +08:00
|
|
|
|
// File dir = new File(basePath+"/"+path);
|
|
|
|
|
// dir.setWritable(true, false);
|
|
|
|
|
// if(!dir.exists()){
|
|
|
|
|
// dir.mkdirs();
|
|
|
|
|
// }
|
2023-11-08 17:15:45 +08:00
|
|
|
|
createDir(basePath + path, sftp);
|
|
|
|
|
sftp.cd(basePath + path);
|
|
|
|
|
// 列出服务器指定的文件列表
|
2023-11-07 09:32:12 +08:00
|
|
|
|
// Vector v = sftp.ls("*");
|
|
|
|
|
// for(int i=0;i<v.size();i++){
|
|
|
|
|
// System.out.println(v.get(i));
|
|
|
|
|
// }
|
2023-11-08 17:15:45 +08:00
|
|
|
|
// 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
sftp.put(fileI, fileName);
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
// 关流操作
|
|
|
|
|
if (outstream != null) {
|
|
|
|
|
outstream.flush();
|
|
|
|
|
outstream.close();
|
|
|
|
|
}
|
|
|
|
|
if (session != null) {
|
|
|
|
|
session.disconnect();
|
|
|
|
|
}
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
channel.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-12-18 16:14:27 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
public static void createDir(String createpath, ChannelSftp sftp) {
|
|
|
|
|
try {
|
|
|
|
|
if (isDirExist(createpath, sftp)) {
|
|
|
|
|
sftp.cd(createpath);
|
|
|
|
|
}
|
|
|
|
|
String pathArry[] = createpath.split("/");
|
|
|
|
|
StringBuffer filePath = new StringBuffer("/");
|
|
|
|
|
for (String path : pathArry) {
|
|
|
|
|
if (path.equals("")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
filePath.append(path + "/");
|
|
|
|
|
if (isDirExist(filePath.toString(), sftp)) {
|
|
|
|
|
sftp.cd(filePath.toString());
|
|
|
|
|
} else {
|
|
|
|
|
// 建立目录
|
|
|
|
|
sftp.mkdir(filePath.toString());
|
|
|
|
|
// 进入并设置为当前目录
|
|
|
|
|
sftp.cd(filePath.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sftp.cd(createpath);
|
|
|
|
|
} catch (SftpException e) {
|
|
|
|
|
// throw new SystemException("创建路径错误:" + createpath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
public static boolean isDirExist(String directory, ChannelSftp sftp) {
|
|
|
|
|
boolean isDirExistFlag = false;
|
|
|
|
|
try {
|
|
|
|
|
SftpATTRS sftpATTRS = sftp.lstat(directory);
|
|
|
|
|
isDirExistFlag = true;
|
|
|
|
|
return sftpATTRS.isDir();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (e.getMessage().toLowerCase().equals("no such file")) {
|
|
|
|
|
isDirExistFlag = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return isDirExistFlag;
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除文件
|
|
|
|
|
* Rhidd
|
|
|
|
|
*
|
|
|
|
|
* @throws SftpException
|
|
|
|
|
* @throws JSchException
|
|
|
|
|
*/
|
|
|
|
|
public static void deleteFile(String directoryFile) throws Exception {
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
Session session = null;
|
|
|
|
|
JSch jsch = new JSch();
|
|
|
|
|
if (port <= 0) {
|
|
|
|
|
// 连接服务器,采用默认端口
|
|
|
|
|
session = jsch.getSession(user, host);
|
|
|
|
|
} else {
|
|
|
|
|
// 采用指定的端口连接服务器
|
|
|
|
|
session = jsch.getSession(user, host, port);
|
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
// 如果服务器连接不上,则抛出异常
|
|
|
|
|
if (session == null) {
|
|
|
|
|
throw new Exception("session is null");
|
|
|
|
|
}
|
|
|
|
|
// 设置登陆主机的密码
|
|
|
|
|
session.setPassword(password);
|
|
|
|
|
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
|
|
|
|
|
session.setConfig("StrictHostKeyChecking", "no");
|
|
|
|
|
// 设置登陆超时时间
|
|
|
|
|
session.connect(30000);
|
|
|
|
|
// 打开openChannel的sftp
|
|
|
|
|
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
|
|
|
|
|
// 远程连接
|
|
|
|
|
channelSftp.connect();
|
|
|
|
|
// 删除文件
|
|
|
|
|
try {
|
|
|
|
|
Vector<ChannelSftp.LsEntry> vector = channelSftp.ls(basePath + directoryFile);
|
|
|
|
|
if (vector.size() == 1) { // 文件,直接删除
|
|
|
|
|
channelSftp.rm(basePath + directoryFile);
|
|
|
|
|
System.out.println("4、" + directoryFile + " 删除的文件.....");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2023-12-18 16:14:27 +08:00
|
|
|
|
throw new RuntimeException(e);
|
2023-11-08 17:15:45 +08:00
|
|
|
|
}
|
|
|
|
|
// 切断远程连接
|
|
|
|
|
channelSftp.exit();
|
2023-11-07 09:32:12 +08:00
|
|
|
|
|
2023-11-08 17:15:45 +08:00
|
|
|
|
}
|
2023-11-07 09:32:12 +08:00
|
|
|
|
}
|