配置文件
application.properties
# 服务器用户
server-username=
# 服务器密码
server-password=
# 服务器ip
server-host=
# 服务器端口
server-port=22
# 需要上传的服务器路径
server-uploadAddres=
# 本地监控路径
server-localhostPath=
# 备份文件夹
server-backups=
ListeningFile
@Slf4j
@Component
public class ListeningFile {
private static String localhostPath;
@Value("${server-localhostPath}")
public void setLocalhostPath(String localhostPath) {
this.localhostPath = localhostPath;
}
public static void getFile() throws FileNotFoundException, IOException {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
WatchKey key;
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
// 新增、删除、修改
Paths.get(localhostPath).register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
File file = new File(localhostPath);//path为监听文件夹
File[] files = file.listFiles();
System.out.println("等待文件加载!");
key = watchService.take();//没有文件增加时,阻塞在这里
for (WatchEvent> event : key.pollEvents()) {
String fileName = localhostPath + "/" + event.context();
WatchEvent.Kind> type = event.kind();
log.info("文件名 " + event.context() + " 文件类型 " + type.toString());
File file1 = new File(fileName);//获取最新文件
if ("ENTRY_CREATE".equals(type.toString())) {
// 上传服务器
log.info("上传服务器");
UploadServer.uploadFile(file1, UploadServer.uploadAddres);
// 上传备份
UploadServer.backupsFile(file1);
} else if ("ENTRY_DELETE".equals(type.toString())) {
// 删除服务器数据
// log.info("删除服务器数据");
// UploadServer.deleteFile(file1, UploadServer.uploadAddres);
} else if ("ENTRY_MODIFY".equals(type.toString())) {
// 修改服务器数据
// log.info("修改服务器数据");
UploadServer.updateFile(file1, UploadServer.uploadAddres);
}
}
if (!key.reset()) {
break; //中断循环
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 2000, 3000);//第一个数字2000表示,2000ms以后开启定时器,第二个数字3000,表示3000ms后运行一次run
}
}
UploadServer
@Slf4j
@Component
public class UploadServer {
private static String userName;
@Value("${server-username}")
public void setUserName(String userName) {
this.userName = userName;
}
private static String password;
@Value("${server-password}")
public void setPassword(String password) {
this.password = password;
}
private static String host;
@Value("${server-host}")
public void setHost(String host) {
this.host = host;
}
private static int port;
@Value("${server-port}")
public void setPort(int port) {
this.port = port;
}
public static String uploadAddres;
@Value("${server-uploadAddres}")
public void setUploadAddres(String uploadAddres) {
this.uploadAddres = uploadAddres;
}
public static String backups;
@Value("${server-backups}")
public void setBackups(String backups) {
this.backups = backups;
}
// public final static String uploadAddres = System.getProperty("catalina.home")+"\\webapps\\TiaoZhanB\\uploadFile";
// public final static String uploadAddres = "/opt/llgj";
/**
* 上传
*
* @param file 文件
* @param dir 服务器路径
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static String uploadFile(File file, String dir) throws FileNotFoundException, IOException {
SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
sftp.login();
String imgURL = null;
try {
InputStream in = new FileInputStream(file);
System.out.println("服务器路径:" + dir);
// 获取文件名称
String fileName = file.getName();
// 路径和文件名丢进file对象里
// File uploadFile = new File(dir, fileName);
String name = fileName + ".tmp";
sftp.upload(dir, name, in);
in.close();
log.info("文件上传成功 " + dir + "/" + fileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
sftp.logout();
}
return imgURL;
}
/**
* 删除
*
* @param file 文件路径
* @param dir 服务器路径
*/
public static void deleteFile(File file, String dir) {
SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
sftp.login();
String sftpFileName = null;
try {
log.info("服务器路径:" + dir);
// 获取文件名称
String fileName = file.getName();
// 重复覆盖
Vector> listFiles = sftp.listFiles(uploadAddres);
for (Object listFile : listFiles) {
if (listFile instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) listFile;
String oldFileName = entry.getFilename();
if (fileName.equals(oldFileName)) {
sftpFileName = oldFileName;
}
}
}
if (fileName.equals(sftpFileName)) {
// log.info("删除 路径 "+oldFileName+" 上传文件 "+fileName);
// 存在则删除文件
sftp.delete(uploadAddres + "/", fileName);
log.info("文件删除成功 " + uploadAddres + "/", fileName);
} else {
log.info("服务器:" + uploadAddres + "目录不存在此文件");
}
} catch (Exception e) {
e.printStackTrace();
log.info("删除文件失败 " + e);
} finally {
sftp.logout();
}
}
/**
* 修改
*
* @param file 文件路径
* @param dir 服务器路径
*/
public static void updateFile(File file, String dir) {
SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
sftp.login();
try {
InputStream in = new FileInputStream(file);
// 获取文件名称
String fileName = file.getName();
// 获取日期
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyy年MM月dd日");
String reFileName = "";
// 添加日期
if (fileName.contains(".")) {
// 存在操作
String head = fileName.substring(0, fileName.lastIndexOf("."));
String tail = fileName.substring(fileName.lastIndexOf("."), fileName.length());
reFileName = head + "-" + format.format(new Date()) + tail;
} else if (!fileName.contains(".")) {
// 不存在操作
reFileName = fileName + "-" + format.format(new Date());
}
// 存放
List list = new ArrayList<>();
String name = "";
// 重复覆盖,修改
Vector> listFiles = sftp.listFiles(uploadAddres);
for (Object listFile : listFiles) {
if (listFile instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) listFile;
String oldFileName = entry.getFilename();
// log.info("新版文件名 " + fileName + " 旧版文件名 " + oldFileName);
// 根据名字是否存在,在进行修改
if (reFileName.equals(oldFileName)) {
list.add(oldFileName);
log.info("修改 路径 " + oldFileName + " 上传文件 " + fileName);
// 存在则删除文件
sftp.delete(uploadAddres + "/", oldFileName);
log.info("服务器删除文件:" + uploadAddres + "/" + oldFileName);
// 重新添加
sftp.upload(dir, fileName, in);
log.info("文件修改重新上传 路径:" + dir + " 文件:" + reFileName);
// 文件备份
UploadServer.backupsFile(file, fileName);
log.info("文件修改备份 " + file);
// log.info("文件修改 " + backups + "/", fileName);
}
}
}
// 文件不存在,重新上传和备份
if (list.isEmpty()) {
String userName = fileName + ".tmp";
sftp.upload(dir, userName, in);
UploadServer.backupsFile(file);
}
// 关闭输入流输出流,释放内存
in.close();
} catch (Exception e) {
e.printStackTrace();
log.info("修改文件失败 " + e);
} finally {
sftp.logout();
}
}
/**
* 备份文件
*
* @param path
*/
public static void backupsFile(File path) {
try {
File file = new File(backups);
if (!file.exists()) {
// 不存在创建
file.mkdir();
log.info("创建文件夹 " + file);
}
// 文件备份
String pathName = path.getName();
File newFile = new File(backups + "/" + pathName + ".tmp");
// log.info("旧路径 "+path+" 新路径 "+newFile);
FileUtils.copyFile(path, newFile);
String replace = pathName.replace(".tmp", "");
File file1 = new File(backups + "/" + replace);
// 文件存在去除.tmp
if (newFile.exists()) {
// 修改名字
newFile.renameTo(file1);
log.info("备份改名 " + file1);
}
// 文件存在,添加日期
if (file1.exists()) {
// 获取日期
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyy年MM月dd日");
// 添加日期
// 文件名添加日期
if (replace.contains(".")) {
// 存在操作
String head = replace.substring(0, replace.lastIndexOf("."));
String tail = replace.substring(replace.lastIndexOf("."), replace.length());
String name = head + "-" + format.format(new Date()) + tail;
// 修改名字
File file2 = new File(backups + "/" + name);
file1.renameTo(file2);
} else if (!replace.contains(".")) {
// 不存在操作
String name = replace + "-" + format.format(new Date());
// 修改名字
File file2 = new File(backups + "/" + name);
file1.renameTo(file2);
}
}
if (path.exists()) {
path.delete();
log.info("删除监控本地文件 " + path);
}
} catch (Exception e) {
e.printStackTrace();
log.info("备份文件 " + e);
}
}
/**
* 修改备份
*
* @param path 旧文件路径
* @param reFileName 新文件名
*/
public static void backupsFile(File path, String reFileName) {
try {
File file = new File(backups);
if (!file.exists()) {
// 不存在创建
file.mkdir();
log.info("创建文件夹 " + file);
}
File file3 = new File(backups + "/" + reFileName);
if (file3.exists()) {
// 存在删除
file3.delete();
}
// 文件备份
String pathName = path.getName();
File newFile = new File(backups + "/" + pathName + ".tmp");
// log.info("旧路径 "+path+" 新路径 "+newFile);
FileUtils.copyFile(path, newFile);
String replace = pathName.replace(".tmp", "");
File file1 = new File(backups + "/" + replace);
// 文件存在去除.tmp
if (newFile.exists()) {
// 修改名字
newFile.renameTo(file1);
log.info("备份改名 " + file1);
}
// 文件存在,添加日期
if (file1.exists()) {
// 获取日期
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyy年MM月dd日");
// 添加日期
// 文件名添加日期
if (replace.contains(".")) {
// 存在操作
String head = replace.substring(0, replace.lastIndexOf("."));
String tail = replace.substring(replace.lastIndexOf("."), replace.length());
String name = head + "-" + format.format(new Date()) + tail;
// 修改名字
File file2 = new File(backups + "/" + name);
file1.renameTo(file2);
} else if (!replace.contains(".")) {
// 不存在操作
String name = replace + "-" + format.format(new Date());
// 修改名字
File file2 = new File(backups + "/" + name);
file1.renameTo(file2);
}
}
if (path.exists()) {
path.delete();
log.info("删除监控本地文件 " + path);
}
// log.info("备份文件成功 "+newFile);
} catch (Exception e) {
e.printStackTrace();
log.info("备份文件 " + e);
}
}
}
SFTPUtil
public class SFTPUtil {
private transient Logger log = LoggerFactory.getLogger(this.getClass());
private ChannelSftp sftp;
private Session session;
/**
* FTP 登录用户名
*/
private String userName;
/**
* FTP 登录密码
*/
private String password;
/**
* 私钥
*/
private String privateKey;
/**
* FTP 服务器地址IP地址
*/
private String host;
/**
* FTP 端口
*/
private int port;
/**
* 构造基于密码认证的sftp对象
*
* @param userName
* @param password
* @param host
* @param port
*/
public SFTPUtil(String userName, String password, String host, int port) {
this.userName = userName;
this.password = password;
this.host = host;
this.port = port;
}
/**
* 构造基于秘钥认证的sftp对象
*
* @param userName
* @param host
* @param port
* @param privateKey
*/
public SFTPUtil(String userName, String host, int port, String privateKey) {
this.userName = userName;
this.host = host;
this.port = port;
this.privateKey = privateKey;
}
public SFTPUtil() {
}
/**
* 连接sftp服务器
*
* @throws Exception
*/
public void login() {
try {
JSch jsch = new JSch();
if (privateKey != null) {
jsch.addIdentity(privateKey);// 设置私钥
log.info("sftp connect,path of private key file:{}", privateKey);
}
log.info("sftp connect by host:{} userName:{}", host, userName);
session = jsch.getSession(userName, host, port);
log.info("Session is build");
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
log.info("Session is connected");
Channel channel = session.openChannel("sftp");
channel.connect();
log.info("channel is connected");
sftp = (ChannelSftp) channel;
log.info(String.format("sftp server host:[%s] port:[%s] is connect successfull", host, port));
} catch (JSchException e) {
log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", new Object[]{host, port, e.getMessage()});
}
}
/**
* 关闭连接 server
*/
public void logout() {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
log.info("sftp is closed already");
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
log.info("sshSession is closed already");
}
}
}
/**
* 将输入流的数据上传到sftp作为文件
*
* @param directory 上传到该目录
* @param sftpFileName sftp端文件名
* @param input 输入流
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, InputStream input) throws SftpException {
try {
sftp.cd(directory);
} catch (SftpException e) {
log.warn("directory is not exist");
sftp.mkdir(directory);
sftp.cd(directory);
}
sftp.put(input, sftpFileName);
// 文件上传成功修改名字
String newName = sftpFileName.replace(".tmp", "");
String name = newName;
// 获取日期
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyy年MM月dd日");
// 文件名添加日期
if (newName.contains(".")) {
// 存在操作
String head = newName.substring(0, newName.lastIndexOf("."));
String tail = newName.substring(newName.lastIndexOf("."), newName.length());
name = head + "-" + format.format(new Date()) + tail;
} else if (!newName.contains(".")) {
// 不存在操作
name = newName + "-" + format.format(new Date());
}
log.info("旧文件名字:" + sftpFileName + " 新文件名字:" + name);
sftp.rename(sftpFileName, name);
log.info("file:{} is upload successful", sftpFileName);
}
/**
* 上传单个文件
*
* @param directory 上传到sftp目录
* @param uploadFile 要上传的文件,包括路径
* @throws FileNotFoundException
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String uploadFile) throws FileNotFoundException, SftpException {
File file = new File(uploadFile);
upload(directory, file.getName(), new FileInputStream(file));
}
/**
* 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。
*
* @param directory 上传到sftp目录
* @param sftpFileName 文件在sftp端的命名
* @param byteArr 要上传的字节数组
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, byte[] byteArr) throws SftpException {
upload(directory, sftpFileName, new ByteArrayInputStream(byteArr));
}
/**
* 将字符串按照指定的字符编码上传到sftp
*
* @param directory 上传到sftp目录
* @param sftpFileName 文件在sftp端的命名
* @param dataStr 待上传的数据
* @param charsetName sftp上的文件,按该字符编码保存
* @throws UnsupportedEncodingException
* @throws SftpException
* @throws Exception
*/
public void upload(String directory, String sftpFileName, String dataStr, String charsetName) throws UnsupportedEncodingException, SftpException {
upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
}
/**
* 下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
* @throws SftpException
* @throws FileNotFoundException
* @throws Exception
*/
public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException {
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
log.info("file:{} is download successful", downloadFile);
}
/**
* 下载文件
*
* @param directory 下载目录
* @param downloadFile 下载的文件名
* @return 字节数组
* @throws SftpException
* @throws IOException
* @throws Exception
*/
public byte[] download(String directory, String downloadFile) throws SftpException, IOException {
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
InputStream is = sftp.get(downloadFile);
byte[] fileData = IOUtils.toByteArray(is);
log.info("file:{} is download successful", downloadFile);
return fileData;
}
/**
* 删除文件
*
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
* @throws SftpException
* @throws Exception
*/
public void delete(String directory, String deleteFile) throws SftpException {
sftp.cd(directory);
sftp.rm(deleteFile);
}
/**
* 列出目录下的文件
*
* @param directory 要列出的目录
* @return
* @throws SftpException
*/
public Vector> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
public void updateName(String directory, String oldFileNm, String newFileNm) {
try {
sftp.cd(directory);
sftp.rename(oldFileNm, newFileNm);
} catch (Exception e) {
e.printStackTrace();
log.info("文件名修改错误 " + e);
}
}
/**
* 备份全部文件
*
* @param directory 目录
* @throws SftpException
*/
public List backupsFiles(String directory) throws SftpException {
List fileList = new ArrayList<>();
sftp.cd(directory);
Vector ls = sftp.ls(directory);
for (Object file : ls) {
if (file instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file;
String oldFileName = entry.getFilename();
fileList.add(oldFileName);
}
}
return fileList;
}
}