fix:smb空指针问题修复
parent
8fc5375db9
commit
1197111074
|
@ -4,7 +4,9 @@ import java.io.InputStream;
|
|||
import java.io.OutputStream;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.jcraft.jsch.Channel;
|
||||
|
@ -15,18 +17,36 @@ import com.jcraft.jsch.Session;
|
|||
import com.jcraft.jsch.SftpATTRS;
|
||||
import com.jcraft.jsch.SftpException;
|
||||
|
||||
@Configuration
|
||||
public class Smb {
|
||||
|
||||
@Value("${smb.host}")
|
||||
private static String host;
|
||||
@Value("${smb.port}")
|
||||
private static Integer port ;
|
||||
@Value("${smb.user}")
|
||||
private static String user;
|
||||
@Value("${smb.password}")
|
||||
private static String password;
|
||||
@Value("${smb.basePath}")
|
||||
private static String basePath;
|
||||
|
||||
public static String host;
|
||||
public static Integer port;
|
||||
public static String user;
|
||||
public static String password;
|
||||
public static String basePath;
|
||||
|
||||
@Value("${smb.host}")
|
||||
public void setHostPath(String hostProperties){
|
||||
host = hostProperties;
|
||||
}
|
||||
@Value("${smb.port}")
|
||||
public void setPortPath(Integer portProperties){
|
||||
port = portProperties;
|
||||
}
|
||||
@Value("${smb.user}")
|
||||
public void setUserPath(String userProperties){
|
||||
user = userProperties;
|
||||
}
|
||||
@Value("${smb.password}")
|
||||
public void setPasswordPath(String passwordProperties){
|
||||
password = passwordProperties;
|
||||
}
|
||||
@Value("${smb.basePath}")
|
||||
public void setBasePath(String basePathProperties){
|
||||
basePath = basePathProperties;
|
||||
}
|
||||
|
||||
// private static String host = "39.103.224.166";
|
||||
//
|
||||
|
@ -47,221 +67,221 @@ public class Smb {
|
|||
//
|
||||
// private static String basePath = "/mnt/qask/file/";
|
||||
|
||||
public static final String SMBBAES_PATH = basePath;
|
||||
public static final String SMBBAES_PATH = basePath;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
// 如果服务器连接不上,则抛出异常
|
||||
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;
|
||||
|
||||
// 如果服务器连接不上,则抛出异常
|
||||
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;
|
||||
// 进入服务器指定的文件夹
|
||||
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);
|
||||
// 列出服务器指定的文件列表
|
||||
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(file.getInputStream(), fileName);
|
||||
sftp.put(file.getInputStream(), fileName);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关流操作
|
||||
if (outstream != null) {
|
||||
outstream.flush();
|
||||
outstream.close();
|
||||
}
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
}
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关流操作
|
||||
if (outstream != null) {
|
||||
outstream.flush();
|
||||
outstream.close();
|
||||
}
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
}
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
// 如果服务器连接不上,则抛出异常
|
||||
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;
|
||||
// 如果服务器连接不上,则抛出异常
|
||||
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;
|
||||
// 进入服务器指定的文件夹
|
||||
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);
|
||||
// 列出服务器指定的文件列表
|
||||
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);
|
||||
sftp.put(fileI, fileName);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关流操作
|
||||
if (outstream != null) {
|
||||
outstream.flush();
|
||||
outstream.close();
|
||||
}
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
}
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关流操作
|
||||
if (outstream != null) {
|
||||
outstream.flush();
|
||||
outstream.close();
|
||||
}
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
}
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*Rhidd
|
||||
* @throws SftpException
|
||||
* @throws JSchException
|
||||
*/
|
||||
public static void deleteFile(String directoryFile) throws Exception {
|
||||
/**
|
||||
* 删除文件
|
||||
* Rhidd
|
||||
*
|
||||
* @throws SftpException
|
||||
* @throws JSchException
|
||||
*/
|
||||
public static void deleteFile(String directoryFile) throws Exception {
|
||||
|
||||
Session session = null;
|
||||
JSch jsch = new JSch();
|
||||
if (port <= 0) {
|
||||
// 连接服务器,采用默认端口
|
||||
session = jsch.getSession(user, host);
|
||||
} else {
|
||||
// 采用指定的端口连接服务器
|
||||
session = jsch.getSession(user, host, port);
|
||||
}
|
||||
Session session = 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);
|
||||
// 打开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) {
|
||||
// 如果服务器连接不上,则抛出异常
|
||||
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) {
|
||||
|
||||
}
|
||||
// 切断远程连接
|
||||
channelSftp.exit();
|
||||
}
|
||||
// 切断远程连接
|
||||
channelSftp.exit();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue