ChannelSftp sftpClient = JschSFTPUtils.getSftpClient(用户名, 密码, 端口号, ip);
JschSFTPUtils.uploadFile(sftpClient,file.toString()这个是本地文件路径 , 远程服务器的目录);
StringBuffer sqlbuffers = new StringBuffer();
StringBuffer sqlbuffer = new StringBuffer();
sqlbuffer.append("copy tazu.c_tazu_df_import");
sqlbuffer.append(" from '");
sqlbuffer.append("/home/gpadmin/dianfei/");
sqlbuffer.append(file.getName());
sqlbuffer.append("' WITH csv header delimiter '$' NULL '';");
sqlbuffers.append(sqlbuffer.toString());
if (StringUtils.isBlank(sqlbuffers.toString())) {
throw new NullPointerException();
}
baseMapper.insertCTazuDfImport(sqlbuffers.toString());
@DataSource(DataSourceEnum.DB3)这个是需要切换的超级管理员账户
void insertCTazuDfImport(String sql);
<insert id="insertCTazuDfImport">
${_parameter}
</insert>
package cc.mrbird.system.utils;
import com.jcraft.jsch.*;
import freemarker.log.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
public class JschSFTPUtils {
private static final Logger log = Logger.getLogger(JschSFTPUtils.class.getName());
public static ChannelSftp getSftpClient(String user, String password, int port, String hostname) {
log.info("获取SFTP连接,正在连接服务器。。。。");
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
Session sshSession = jsch.getSession(user, hostname, port);
if (password != null && password.length() > 0) {
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;
log.info("连接服务器成功。。。。");
} catch (JSchException e) {
log.info("连接服务器失败。。。。");
}
return sftp;
}
public static boolean checkExists(ChannelSftp sftp, String filePath) {
boolean isExist = false;
int index = filePath.lastIndexOf("/");
String fileName = filePath.substring(index + 1);
String parentDirPath = filePath.substring(0, index + 1);
Vector files = null;
try {
files = listFiles(sftp, parentDirPath);
} catch (Exception e) {
e.printStackTrace();
}
Iterator iterator = files.iterator();
while (iterator.hasNext()) {
ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry) iterator.next();
if (fileName.equals(lsEntry.getFilename())) {
isExist = true;
break;
}
}
return isExist;
}
public static void uploadFile(ChannelSftp sftp, String localFilePath, String goalDirPath) {
try {
if (!checkExists(sftp, goalDirPath)) {
sftp.mkdir(goalDirPath);
}
sftp.cd(goalDirPath);
File localFile = new File(localFilePath);
String fileName = localFile.getName();
sftp.put(new FileInputStream(localFile), fileName);
log.info("上传文件成功,文件名:" + localFilePath);
} catch (Exception e) {
e.printStackTrace();
log.info("上传文件失败,文件名:" + localFilePath);
}
}
public static void upload(ChannelSftp sftp, String localFilePath, String goalDirPath) {
try {
sftp.cd(goalDirPath);
File localFile = new File(localFilePath);
String fileName = localFile.getName();
sftp.put(new FileInputStream(localFile), fileName);
log.info("上传文件成功,文件名:" + localFilePath);
} catch (Exception e) {
e.printStackTrace();
log.info("上传文件失败,文件名:" + localFilePath);
}
}
public static Vector listFiles(ChannelSftp sftp, String dirPath) {
Vector files = null;
try {
files = sftp.ls(dirPath);
} catch (Exception e) {
log.info("获取文件或目录失败。。。");
}
return files;
}
public static void downloadFile(ChannelSftp sftp, String downloadFilePath, String goalDirPath) {
try {
if (checkExists(sftp, downloadFilePath)) {
File downloadFile = new File(downloadFilePath);
File goalFile = new File(goalDirPath, downloadFile.getName());
FileOutputStream os = new FileOutputStream(goalFile);
sftp.get(downloadFilePath, os);
} else {
log.info("文件【" + downloadFilePath + "】不存在~~");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void deleteFile(ChannelSftp sftp, String dirPath, String filePath) {
try {
sftp.cd(dirPath);
sftp.rm(filePath);
} catch (SftpException e) {
e.printStackTrace();
}
}
public static void close(ChannelSftp sftp) {
try {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.getSession().disconnect();
sftp.disconnect();
}
}
} catch (Exception e) {
log.info("关闭sftp失败");
}
log.info("关闭sftp成功");
}
}