FTP功能实现,暂时保留注释信息
This commit is contained in:
@@ -5,10 +5,12 @@ import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.RemoteExecuteProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FtpOperationUtil;
|
||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import com.fjy.spring.untils.RemoteCommandUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -35,6 +37,9 @@ public class DeleteController {
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
@Autowired
|
||||
private RemoteExecuteProperties remoteExecuteProperties;
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
@@ -121,60 +126,31 @@ public class DeleteController {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteFTPFile(TbFile tbFile) {
|
||||
public boolean deleteFTPFile(TbFile tbFile) throws IOException {
|
||||
tbFile.setColfileid(tbFile.getColfileid());
|
||||
String path = "/upload/" + tbFile.getCourseName() + "/" + tbFile.getWorkFolder() + "/";
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
boolean flagExistsFile = false;
|
||||
boolean flagDeleteFile = false;
|
||||
try {
|
||||
ftpOperationUtil.connectServer();
|
||||
flagExistsFile = ftpOperationUtil.isExistsFile(path, tbFile.getColfilename());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String path = fileService.findFileById(tbFile).getColfilepath();
|
||||
|
||||
//直接删除
|
||||
RemoteCommandUtil.deleteFile(path, remoteExecuteProperties);
|
||||
|
||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||
if (flagExistsFile) {
|
||||
System.out.println("【path】" + path);
|
||||
if (flagExistsFile) {
|
||||
try {
|
||||
flagDeleteFile = ftpOperationUtil.deleteFile(path + tbFile.getColfilename());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("【flagDeleteFile】" + flagDeleteFile);
|
||||
}
|
||||
fileService.deleteFileById(tbFile);
|
||||
log.info("删除单个文件" + path + "成功!");
|
||||
|
||||
if (flagDeleteFile) {
|
||||
fileService.deleteFileById(tbFile);
|
||||
log.info("删除单个文件" + path + "成功!");
|
||||
TbUser user = (TbUser) request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
//写入日志信息
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
TbLog log = new TbLog();
|
||||
log.setUserid(user.getColuserid());
|
||||
log.setColtime(dateNowStr);
|
||||
log.setColheader("删除单个文件" + path + "成功!");
|
||||
log.setRequestURL(request.getRequestURL().toString());
|
||||
|
||||
TbUser user = (TbUser) request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
//写入日志信息
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
TbLog log = new TbLog();
|
||||
log.setUserid(user.getColuserid());
|
||||
log.setColtime(dateNowStr);
|
||||
log.setColheader("删除单个文件" + path + "成功!");
|
||||
log.setRequestURL(request.getRequestURL().toString());
|
||||
|
||||
//解决nginx代理后IP地址获取问题
|
||||
log.setColip(GetIPAddrUtil.getIpAddr(request));
|
||||
logService.addLogRec(log);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
log.info("删除单个文件" + path + "失败!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log.info("删除单个文件失败:" + path + tbFile.getColfilename() + "不存在!");
|
||||
return false;
|
||||
}
|
||||
//解决nginx代理后IP地址获取问题
|
||||
log.setColip(GetIPAddrUtil.getIpAddr(request));
|
||||
logService.addLogRec(log);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,10 +9,7 @@ import com.fjy.spring.properties.RemoteExecuteProperties;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FtpOperationUtil;
|
||||
import com.fjy.spring.untils.FtpUtils;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import com.fjy.spring.untils.RemoteExecuteCommandUtil;
|
||||
import com.fjy.spring.untils.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
@@ -167,9 +164,8 @@ public class DownLoadController {
|
||||
|
||||
String ctxPath = tbFile.getColfilepath();
|
||||
String downLoadPath = ctxPath;
|
||||
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||||
try {
|
||||
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||||
|
||||
response.setContentType("application/x-msdownload;");
|
||||
response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1"));
|
||||
|
||||
@@ -207,6 +203,7 @@ public class DownLoadController {
|
||||
if (bos != null) {
|
||||
try {
|
||||
bos.close();
|
||||
ftpUtil.closeServer();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@@ -346,13 +343,6 @@ public class DownLoadController {
|
||||
String desPath = zipPath + "/" + zipFileName;
|
||||
//System.out.println("打包文件存储地址:"+desPath);
|
||||
|
||||
//远程登录SSH
|
||||
RemoteExecuteCommandUtil rec = new RemoteExecuteCommandUtil(
|
||||
remoteExecuteProperties.getIp(),
|
||||
remoteExecuteProperties.getUser(),
|
||||
remoteExecuteProperties.getPassword(),
|
||||
remoteExecuteProperties.getPort());
|
||||
rec.login();
|
||||
//登录FTP
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
ftpOperationUtil.connectServer();
|
||||
@@ -363,11 +353,7 @@ public class DownLoadController {
|
||||
|
||||
String desZipPath = remoteExecuteProperties.getPath() + "zip/";
|
||||
|
||||
String cmd = "zip -r " + desZipPath + zipFileName + " " + remoteExecuteProperties.getPath() + "upload/" + courseName + "/" + folder;
|
||||
System.out.println("【cmd】" + cmd);
|
||||
if (rec.executeSuccess(cmd).equals("")) {
|
||||
log.error("【cmd创建压缩文件失败】" + cmd);
|
||||
}
|
||||
RemoteCommandUtil.zipFile(desZipPath,zipFileName,remoteExecuteProperties,"upload/" + courseName + "/" + folder);
|
||||
|
||||
response.setContentType("application/x-msdownload;");
|
||||
response.setHeader("Content-disposition", "attachment; filename=" + new String(zipFileName.getBytes("utf-8"), "ISO8859-1"));
|
||||
|
||||
@@ -5,35 +5,26 @@ import com.fjy.spring.domain.Homework;
|
||||
import com.fjy.spring.domain.TbFile;
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.properties.RemoteExecuteProperties;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.HomeworkService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FormatFileSizeUtil;
|
||||
import com.fjy.spring.untils.FtpOperationUtil;
|
||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import com.fjy.spring.untils.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItem;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
@@ -48,6 +39,9 @@ public class UpLoadController {
|
||||
@Autowired
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Autowired
|
||||
private RemoteExecuteProperties remoteExecuteProperties;
|
||||
|
||||
/**
|
||||
* 文件相关数据库操作
|
||||
*/
|
||||
@@ -203,8 +197,6 @@ public class UpLoadController {
|
||||
tbFile.setColfilename(filename);
|
||||
}
|
||||
|
||||
File targetFile = new File(pathname);
|
||||
|
||||
log.info("文件上传到: " + uploadUrl + filename);
|
||||
log.info("文件大小: " + FormatFileSizeUtil.GetFileSize(file.getSize()));
|
||||
log.info("文件名: " + filename);
|
||||
@@ -220,60 +212,64 @@ public class UpLoadController {
|
||||
tbFile.setCourseName(courseName);
|
||||
tbFile.setWorkFolder(folder);
|
||||
|
||||
if (fileService.addFile(tbFile)) {
|
||||
log.info("记录写入数据库成功");
|
||||
|
||||
// 记录上传日志
|
||||
addVisitLog("上传了" + tbFile.getColrealname() + "->" + tbFile.getColfilename());
|
||||
} else {
|
||||
log.error("记录写入数据库失败");
|
||||
}
|
||||
|
||||
try {
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
ftpOperationUtil.connectServer();
|
||||
InputStream inputStream = file.getInputStream();
|
||||
String path = "/upload/" + courseName + "/" + folder + "/";
|
||||
boolean flagExistsFile = ftpOperationUtil.isExistsFile(path,tbFile.getColfilename());
|
||||
System.out.println("flagExistsFile"+flagExistsFile);
|
||||
boolean flagExistsFile = ftpOperationUtil.isExistsFile(path, tbFile.getColfilename());
|
||||
System.out.println("flagExistsFile" + flagExistsFile);
|
||||
//若文件已存在则自动重命名
|
||||
if (flagExistsFile){
|
||||
if (flagExistsFile) {
|
||||
String oldFileName;
|
||||
if (rename) {
|
||||
oldFileName = path + "bak/" + user.getColstudentno() + user.getColrealname() + suffix;
|
||||
oldFileName = path + "bak/" + filePrefix + user.getColstudentno() + user.getColrealname() + fileSuffix + suffix;
|
||||
} else {
|
||||
oldFileName = path + "bak/" + filename;
|
||||
}
|
||||
log.info("源文件路径:" + pathname);
|
||||
|
||||
String newFileName = tbFile.getColfilename() + "." + dateNowStr2 + ".bak";
|
||||
// 数据库查找已存在文件的记录
|
||||
TbFile file1 = fileService.findByFilepath(pathname);
|
||||
file1.setColfilepath(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
file1.setColfilename(file1.getColfilename() + "." + dateNowStr2 + ".bak");
|
||||
|
||||
tbFile.setColfilepath(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
tbFile.setColfilename(tbFile.getColfilename() + "." + dateNowStr2 + ".bak");
|
||||
if (fileService.addFile(tbFile)) {
|
||||
String bakPath = path + "bak/";
|
||||
|
||||
System.out.println("【path】" + bakPath + "【originPath】" + pathname +
|
||||
"【filename】" + tbFile.getColfilename() + "【newFileName】" + newFileName);
|
||||
|
||||
RemoteCommandUtil.moveFile(pathname, bakPath, newFileName, remoteExecuteProperties);
|
||||
|
||||
log.info("源文件:" + oldFileName + "已重命名为:" + newFileName);
|
||||
if (fileService.addFile(file1)) {
|
||||
log.info("重命名文件数据库更新成功");
|
||||
} else {
|
||||
log.error("重命名文件数据库更新失败");
|
||||
}
|
||||
File mvfile = new File(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
try {
|
||||
// FileUtils.moveFile(targetFile, mvfile);
|
||||
ftpOperationUtil.renameFile(oldFileName,newFileName);
|
||||
log.info("源文件:" + targetFile.getName() + "已重命名为:" + mvfile.getName());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
boolean flag = ftpOperationUtil.uploadFile(inputStream,
|
||||
tbFile.getColfilename(),path);
|
||||
tbFile.getColfilename(), path);
|
||||
if (fileService.addFile(tbFile)) {
|
||||
log.info("记录写入数据库成功");
|
||||
|
||||
// 记录上传日志
|
||||
addVisitLog("上传了" + tbFile.getColrealname() + "->" + tbFile.getColfilename());
|
||||
} else {
|
||||
log.error("记录写入数据库失败");
|
||||
}
|
||||
ftpOperationUtil.closeServer();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登陆后的访问日志记录
|
||||
*
|
||||
* @param content
|
||||
*/
|
||||
private void addVisitLog(String content) {
|
||||
|
||||
0
src/main/java/com/fjy/spring/controller/temp.java
Normal file
0
src/main/java/com/fjy/spring/controller/temp.java
Normal file
@@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
|
||||
@Data
|
||||
public class FtpProperties {
|
||||
//服务器地址名称
|
||||
private String server = "176.122.138.235";
|
||||
private String server = "104.223.24.81";
|
||||
//端口号
|
||||
private int port = 21;
|
||||
//用户名称
|
||||
|
||||
@@ -141,7 +141,7 @@ public class FtpOperationUtil {
|
||||
* @param multiDirectory
|
||||
* @return
|
||||
*/
|
||||
private boolean createMultiDirectory(String multiDirectory) {
|
||||
public boolean createMultiDirectory(String multiDirectory) {
|
||||
boolean bool = false;
|
||||
try {
|
||||
String[] dirs = multiDirectory.split("/");
|
||||
@@ -223,21 +223,48 @@ public class FtpOperationUtil {
|
||||
/**
|
||||
* 检查目录在服务器上是否存在 true:存在 false:不存在
|
||||
*
|
||||
* @param dirName
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean existDirectory(String dirName) throws IOException {
|
||||
boolean flag = false;
|
||||
FTPFile[] ftpFileArr = ftpClient.listFiles("/");
|
||||
for (FTPFile ftpFile : ftpFileArr) {
|
||||
if (ftpFile.isDirectory()
|
||||
&& ftpFile.getName().equalsIgnoreCase(dirName)) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断给定路径是否存在
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean existDirectory(String path) throws IOException {
|
||||
|
||||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
public boolean existMultiDirectory(String path) throws IOException {
|
||||
boolean flag = false;
|
||||
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
|
||||
for (FTPFile ftpFile : ftpFileArr) {
|
||||
if (ftpFile.isDirectory()
|
||||
&& ftpFile.getName().equalsIgnoreCase(path)) {
|
||||
flag = true;
|
||||
break;
|
||||
|
||||
if (path != null && path.length() != 0) {
|
||||
String[] dirs = path.split("/");
|
||||
ftpClient.changeWorkingDirectory("/");
|
||||
for (int i = 0; dirs != null && i < dirs.length; i++) {
|
||||
FTPFile[] ftpFileArr = ftpClient.listFiles();
|
||||
for (FTPFile ftpFile : ftpFileArr) {
|
||||
// 判断是否为最后一级目录
|
||||
if (ftpFile.isDirectory()
|
||||
&& ftpFile.getName().equalsIgnoreCase(dirs[dirs.length - 1])) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 进入下一级目录
|
||||
ftpClient.changeWorkingDirectory(new String(dirs[i].getBytes("UTF-8"), "iso-8859-1"));
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
@@ -286,6 +313,54 @@ public class FtpOperationUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现文件移动操作
|
||||
* @param desPath
|
||||
* @param originPath
|
||||
* @param oldFileName
|
||||
* @param newFileName
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean moveFile(String desPath, String originPath, String oldFileName,String newFileName) throws IOException {
|
||||
oldFileName = new String(oldFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
newFileName = new String(newFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
boolean moveFlag;
|
||||
|
||||
// 目标文件目录是否存在
|
||||
System.out.println("【desPath】"+desPath);
|
||||
if (!existMultiDirectory(desPath)) {
|
||||
System.out.println("【创建目录】"+createMultiDirectory(desPath));
|
||||
}
|
||||
|
||||
// 切换到源文件目录
|
||||
changWorkingDir(originPath);
|
||||
|
||||
//转换成输入流
|
||||
InputStream is = null;
|
||||
is = ftpClient.retrieveFileStream(oldFileName);
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
|
||||
System.out.println("【inputStream】"+is.available()+" "+bufferedInputStream.available());
|
||||
is.close();
|
||||
boolean cpcflag = ftpClient.completePendingCommand();
|
||||
System.out.println("【completePendingCommand】"+cpcflag);
|
||||
// 切换到目标文件目录
|
||||
changWorkingDir(desPath);
|
||||
//复制文件
|
||||
moveFlag = ftpClient.storeFile(newFileName, bufferedInputStream);
|
||||
System.out.println("【创建目录】"+moveFlag);
|
||||
if (moveFlag) {
|
||||
changWorkingDir(originPath);
|
||||
//删除源文件
|
||||
ftpClient.deleteFile(oldFileName);
|
||||
bufferedInputStream.close();
|
||||
}
|
||||
return moveFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到文件列表,listFiles返回包含目录和文件,它返回的是一个FTPFile数组
|
||||
* listNames():只包含目录的字符串数组
|
||||
@@ -545,38 +620,22 @@ public class FtpOperationUtil {
|
||||
return ftpClient.retrieveFileStream(sourceFileName);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//testUpload();
|
||||
//testDownload();
|
||||
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||||
ftpUtil = new FtpOperationUtil();
|
||||
ftpUtil.connectServer("192.168.79.138", FTPClient.DEFAULT_PORT, "cms", "imis2", null);
|
||||
//获得ftp服务器上目录名称为DF4下的所有文件名称
|
||||
List<String> list = ftpUtil.getFileList("/");
|
||||
for (String str : list) {
|
||||
System.out.println(str);
|
||||
/**
|
||||
* 采用递归方式切换目录
|
||||
*
|
||||
* @param path
|
||||
* @throws IOException
|
||||
*/
|
||||
public void changWorkingDir(String path) throws IOException {
|
||||
if (path != null && path.length() != 0) {
|
||||
String[] dirs = path.split("/");
|
||||
ftpClient.changeWorkingDirectory("/");
|
||||
|
||||
for (int i = 0; dirs != null && i < dirs.length; i++) {
|
||||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||||
ftpClient.changeWorkingDirectory(dirs[i]);
|
||||
System.out.println("【当前目录】" + new String(ftpClient.printWorkingDirectory().getBytes("iso-8859-1"), "UTF-8"));
|
||||
}
|
||||
}
|
||||
// 上传本地D盘文件aaa.txt到服务器,服务器上名称为bbb.txt
|
||||
//ftpUtil.uploadFile("F:\\JAVA Workspace\\Temp\\upload\\ERP实验1:销售预测与SOP.doc", "ERP实验1:销售预测与SOP.doc");
|
||||
// 从服务器上下载文件bbb.txt到本地d盘名称为ccc.txt
|
||||
//ftpUtil.download("ERP实验1:销售预测与SOP.doc", "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc");
|
||||
|
||||
String name = "ERP实验1:销售预测与SOP.doc";
|
||||
InputStream inputStream = ftpUtil.downFile(name);
|
||||
|
||||
String destination = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc";
|
||||
int index;
|
||||
byte[] bytes = new byte[1024];
|
||||
FileOutputStream downloadFile = new FileOutputStream(destination);
|
||||
while ((index = inputStream.read(bytes)) != -1) {
|
||||
downloadFile.write(bytes, 0, index);
|
||||
downloadFile.flush();
|
||||
}
|
||||
downloadFile.close();
|
||||
inputStream.close();
|
||||
|
||||
// 删除ftp服务器上文件:bbb.txt
|
||||
//ftpUtil.deleteFile("bbb.txt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
92
src/main/java/com/fjy/spring/untils/RemoteCommandUtil.java
Normal file
92
src/main/java/com/fjy/spring/untils/RemoteCommandUtil.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package com.fjy.spring.untils;
|
||||
|
||||
import com.fjy.spring.properties.RemoteExecuteProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 封装Linux远程命令
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2018-05-13 09:11
|
||||
*/
|
||||
@Slf4j
|
||||
public class RemoteCommandUtil {
|
||||
|
||||
private static RemoteExecuteCommandUtil REC;
|
||||
private static String cmd;
|
||||
|
||||
/**
|
||||
* 生成压缩文件
|
||||
*
|
||||
* @param desZipPath 压缩文件目标地址
|
||||
* @param zipFileName 压缩文件名
|
||||
* @param remoteExecuteProperties 远程参数配置类
|
||||
* @param originPath 目标文件、文件夹地址
|
||||
*/
|
||||
public static void zipFile(String desZipPath,
|
||||
String zipFileName,
|
||||
RemoteExecuteProperties remoteExecuteProperties,
|
||||
String originPath) {
|
||||
login(remoteExecuteProperties);
|
||||
desZipPath = remoteExecuteProperties.getPath() + desZipPath;
|
||||
originPath = remoteExecuteProperties.getPath() + originPath;
|
||||
cmd = "zip -r " + desZipPath + zipFileName + " " + originPath;
|
||||
log.info("【cmd】" + cmd);
|
||||
log.info("【res】" + REC.execute(cmd));
|
||||
log.error("【cmd创建压缩文件失败】" + cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动文件
|
||||
*
|
||||
* @param originPath 源文件路径
|
||||
* @param desPath 目标路径
|
||||
* @param remoteExecuteProperties 远程参数配置类
|
||||
* @return
|
||||
*/
|
||||
public static void moveFile(String originPath, String desPath, String newName, RemoteExecuteProperties remoteExecuteProperties) {
|
||||
login(remoteExecuteProperties);
|
||||
// 创建目标目录
|
||||
mkdir(desPath, remoteExecuteProperties);
|
||||
|
||||
desPath = remoteExecuteProperties.getPath() + desPath;
|
||||
originPath = remoteExecuteProperties.getPath() + originPath;
|
||||
|
||||
cmd = "mv " + originPath + " " + desPath + newName;
|
||||
log.info("【cmd】" + cmd);
|
||||
log.info("【res】" + REC.execute(cmd));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建多级目录
|
||||
*
|
||||
* @param path
|
||||
* @param remoteExecuteProperties
|
||||
*/
|
||||
public static void mkdir(String path, RemoteExecuteProperties remoteExecuteProperties) {
|
||||
login(remoteExecuteProperties);
|
||||
path = remoteExecuteProperties.getPath() + path;
|
||||
cmd = "mkdir -p " + path;
|
||||
log.info("【cmd】" + cmd);
|
||||
log.info("【res】" + REC.execute(cmd));
|
||||
}
|
||||
|
||||
public static void deleteFile(String path, RemoteExecuteProperties remoteExecuteProperties){
|
||||
login(remoteExecuteProperties);
|
||||
path = remoteExecuteProperties.getPath() + path;
|
||||
cmd = "rm -f " + path;
|
||||
log.info("【cmd】" + cmd);
|
||||
log.info("【res】" + REC.execute(cmd));
|
||||
}
|
||||
|
||||
private static void login(RemoteExecuteProperties remoteExecuteProperties) {
|
||||
//远程登录SSH
|
||||
REC = new RemoteExecuteCommandUtil(
|
||||
remoteExecuteProperties.getIp(),
|
||||
remoteExecuteProperties.getUser(),
|
||||
remoteExecuteProperties.getPassword(),
|
||||
remoteExecuteProperties.getPort());
|
||||
REC.login();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,13 +7,13 @@ server:
|
||||
port: 8080
|
||||
serverproperties:
|
||||
port_num: 8080
|
||||
filePath: F:\JAVA Workspace\Temp\
|
||||
filePath: /
|
||||
remoteproperties:
|
||||
ip: 176.122.138.235
|
||||
ip: 104.223.24.81
|
||||
port: 26460
|
||||
user: root
|
||||
password: B171b0GsMCD8
|
||||
path: /home/ftp/cms/
|
||||
password: 27894869root
|
||||
path: /www/wwwroot/cmsftp
|
||||
spring:
|
||||
thymeleaf:
|
||||
prefix: classpath:/templates/
|
||||
|
||||
@@ -62,12 +62,35 @@ public class FtpOperationUtilTest {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
boolean flag = ftpUtil.existDirectory("创建目录");
|
||||
System.out.println("existDirectory1:" + flag);
|
||||
createDirectory();
|
||||
ftpUtil.createDirectory("创建目录");
|
||||
flag = ftpUtil.existDirectory("创建目录");
|
||||
System.out.println("existDirectory2:" + flag);
|
||||
ftpUtil.removeDirectory("创建目录");
|
||||
listFiles(ftpUtil);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changWorkingDir() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
ftpUtil.changWorkingDir("/upload/信息安全/时间测试/");
|
||||
String[] lists = ftpUtil.list();
|
||||
for (String list : lists) {
|
||||
System.out.println(list);
|
||||
}
|
||||
ftpUtil.changWorkingDir("/");
|
||||
String[] lists2 = ftpUtil.list();
|
||||
for (String list : lists2) {
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existMultiDirectory() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
boolean flag = ftpUtil.existMultiDirectory("/upload/信息安全/时间测试/");
|
||||
System.out.println("existMultiDirectory:" + flag);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isExistsFile() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
@@ -85,7 +108,7 @@ public class FtpOperationUtilTest {
|
||||
@Test
|
||||
public void list() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
String[] lists = ftpUtil.list("/upload/信息安全/时间测试/");
|
||||
String[] lists = ftpUtil.list("/upload/");
|
||||
for (String list : lists) {
|
||||
System.out.println(list);
|
||||
}
|
||||
@@ -127,4 +150,13 @@ public class FtpOperationUtilTest {
|
||||
ftpUtil.download(FILE_NAME, DESTINATION);
|
||||
listFiles(ftpUtil);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moveFile() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
String desPath = "/upload/bak/";
|
||||
String originPath = "/upload/";
|
||||
ftpUtil.moveFile(desPath,originPath,FILE_NAME,FILE_NAME);
|
||||
listFiles(ftpUtil);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.fjy.spring.untils;
|
||||
|
||||
import com.fjy.spring.properties.RemoteExecuteProperties;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class RemoteCommandUtilTest {
|
||||
private RemoteExecuteProperties rep = new RemoteExecuteProperties();
|
||||
private boolean flag;
|
||||
|
||||
@Test
|
||||
public void zipFile() {
|
||||
dataSet();
|
||||
String desZipPath = "/test/";
|
||||
String zipFileName = "test.zip";
|
||||
String originPath = "/upload/";
|
||||
RemoteCommandUtil.zipFile(desZipPath, zipFileName, rep, originPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void moveFile() {
|
||||
dataSet();
|
||||
String desPath = "/test/zip/";
|
||||
String newName = "test.zip";
|
||||
String originPath = "/test/test.zip";
|
||||
RemoteCommandUtil.moveFile(originPath,desPath,newName,rep);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mkdir() {
|
||||
dataSet();
|
||||
String path = "/test/zip/";
|
||||
RemoteCommandUtil.mkdir(path,rep);
|
||||
}
|
||||
|
||||
private void dataSet() {
|
||||
rep.setIp("104.223.24.81");
|
||||
rep.setPassword("27894869root");
|
||||
rep.setUser("root");
|
||||
rep.setPath("/www/wwwroot/cmsftp");
|
||||
rep.setPort(26460);
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,12 @@ import static org.junit.Assert.*;
|
||||
|
||||
public class RemoteExecuteCommandUtilTest {
|
||||
|
||||
RemoteExecuteCommandUtil rec=new RemoteExecuteCommandUtil("176.122.138.235", "root","B171b0GsMCD8",26460);
|
||||
RemoteExecuteCommandUtil rec=new RemoteExecuteCommandUtil("104.223.24.81", "root","27894869root",26460);
|
||||
|
||||
@Test
|
||||
public void login() {
|
||||
rec.login();
|
||||
System.out.println(rec.execute("ifconfig"));
|
||||
System.out.println(rec.execute("cd /1/2/3/4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user