FTP删除、重命名成功
This commit is contained in:
@@ -7,6 +7,7 @@ import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@@ -41,18 +43,19 @@ public class DeleteController {
|
||||
|
||||
@GetMapping("/home/findfile")
|
||||
@ResponseBody
|
||||
public String findFilePath(@RequestParam(value = "fileid") Integer fileid){
|
||||
public String findFilePath(@RequestParam(value = "fileid") Integer fileid) {
|
||||
TbFile tbFile = new TbFile();
|
||||
tbFile.setColfileid(fileid);
|
||||
TbFile resfile = fileService.findFileById(tbFile);
|
||||
return resfile.getColfilepath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件,可以是文件或文件夹
|
||||
*/
|
||||
@RequestMapping("/home/filedelete")
|
||||
@ResponseBody
|
||||
public boolean delete(@RequestParam(value = "fileid") Integer fileid)throws Exception {
|
||||
public boolean delete(@RequestParam(value = "fileid") Integer fileid) throws Exception {
|
||||
TbFile tbFile = new TbFile();
|
||||
tbFile.setColfileid(fileid);
|
||||
TbFile resfile = fileService.findFileById(tbFile);
|
||||
@@ -61,10 +64,10 @@ public class DeleteController {
|
||||
log.error("删除文件失败:" + resfile.getColfilename() + "不存在!");
|
||||
return false;
|
||||
} else {
|
||||
if (filepath.isFile()){
|
||||
return deleteFile(resfile.getColfilepath(),resfile.getColfileid());
|
||||
}
|
||||
else{
|
||||
if (filepath.isFile()) {
|
||||
return deleteFTPFile(resfile);
|
||||
// return deleteFile(resfile.getColfilepath(), resfile.getColfileid());
|
||||
} else {
|
||||
return deleteDirectory(resfile.getColfilepath());
|
||||
}
|
||||
|
||||
@@ -76,17 +79,32 @@ public class DeleteController {
|
||||
/**
|
||||
* 删除单个文件
|
||||
*/
|
||||
public boolean deleteFile(String fileName,Integer fileid) {
|
||||
public boolean deleteFile(String fileName, Integer fileid) {
|
||||
File file = new File(fileName);
|
||||
TbFile tbFile = new TbFile();
|
||||
tbFile.setColfileid(fileid);
|
||||
|
||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||
if (file.exists() && file.isFile()) {
|
||||
try {
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
ftpOperationUtil.connectServer();
|
||||
System.out.println("【fileName】" + fileName);
|
||||
|
||||
boolean flagExistsFile = ftpOperationUtil.isExistsFile(fileName, tbFile.getColfilename());
|
||||
if (flagExistsFile) {
|
||||
boolean flagDeleteFile = ftpOperationUtil.deleteFile(fileName);
|
||||
System.out.println("【flagDeleteFile】" + flagDeleteFile);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (file.delete()) {
|
||||
fileService.deleteFileById(tbFile);
|
||||
log.info("删除单个文件" + fileName + "成功!");
|
||||
|
||||
TbUser user =(TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbUser user = (TbUser) request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
//写入日志信息
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
@@ -112,11 +130,67 @@ public class DeleteController {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteFTPFile(TbFile tbFile) {
|
||||
tbFile.setColfileid(tbFile.getColfileid());
|
||||
String path = "/upload/" + tbFile.getCourseName() + "/" + tbFile.getWorkFolder() + "/" + tbFile.getColfilename();
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
boolean flagExistsFile = false;
|
||||
boolean flagDeleteFile = false;
|
||||
try {
|
||||
ftpOperationUtil.connectServer();
|
||||
flagExistsFile = ftpOperationUtil.isExistsFile(path, tbFile.getColfilename());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||
if (flagExistsFile) {
|
||||
System.out.println("【path】" + path);
|
||||
if (flagExistsFile) {
|
||||
try {
|
||||
flagDeleteFile = ftpOperationUtil.deleteFile(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("【flagDeleteFile】" + flagDeleteFile);
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
//解决nginx代理后IP地址获取问题
|
||||
log.setColip(GetIPAddrUtil.getIpAddr(request));
|
||||
logService.addLogRec(log);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
log.info("删除单个文件" + path + "失败!");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log.info("删除单个文件失败:" + path + "不存在!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除目录及目录下的文件
|
||||
*功能暂时不开放
|
||||
* @param path
|
||||
* 要删除的目录的文件路径
|
||||
* 功能暂时不开放
|
||||
*
|
||||
* @param path 要删除的目录的文件路径
|
||||
* @return 目录删除成功返回true,否则返回false
|
||||
*/
|
||||
public boolean deleteDirectory(String path) {
|
||||
|
||||
@@ -267,7 +267,35 @@ public class UpLoadController {
|
||||
ftpOperationUtil.connectServer();
|
||||
InputStream inputStream = file.getInputStream();
|
||||
String path = "/upload/" + courseName + "/" + folder + "/";
|
||||
System.out.println("【目录】"+path);
|
||||
boolean flagExistsFile = ftpOperationUtil.isExistsFile(path,tbFile.getColfilename());
|
||||
System.out.println("flagExistsFile"+flagExistsFile);
|
||||
if (flagExistsFile){
|
||||
String oldFileName;
|
||||
if (rename) {
|
||||
oldFileName = path + "bak/" + user.getColstudentno() + user.getColrealname() + suffix;
|
||||
} else {
|
||||
oldFileName = path + "bak/" + filename;
|
||||
}
|
||||
log.info("源文件路径:" + pathname);
|
||||
|
||||
String newFileName = tbFile.getColfilename() + "." + dateNowStr2 + ".bak";
|
||||
|
||||
tbFile.setColfilepath(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
tbFile.setColfilename(tbFile.getColfilename() + "." + dateNowStr2 + ".bak");
|
||||
/*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);
|
||||
} catch (IOException e) {
|
||||
@@ -283,7 +311,6 @@ public class UpLoadController {
|
||||
|
||||
try {
|
||||
file.transferTo(targetFile);
|
||||
// boolean flag = ftpOperationUtil.uploadFile(tbFile.getColfilepath(),tbFile.getColfilename());
|
||||
fileList.add(
|
||||
request.getScheme() + "://" + request.getServerName() + ":"
|
||||
+ serverProperties.getPortNum() + request.getContextPath() + "/upload/"
|
||||
|
||||
@@ -149,7 +149,7 @@ public class FtpOperationUtil {
|
||||
|
||||
//按顺序检查目录是否存在,不存在则创建目录
|
||||
for (int i = 1; dirs != null && i < dirs.length; i++) {
|
||||
dirs[i]= new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||||
if (!ftpClient.changeWorkingDirectory(dirs[i])) {
|
||||
if (ftpClient.makeDirectory(dirs[i])) {
|
||||
if (!ftpClient.changeWorkingDirectory(dirs[i])) {
|
||||
@@ -254,6 +254,36 @@ public class FtpOperationUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看指定路径下是否存在该文件
|
||||
*
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public boolean isExistsFile(String path, String fileName) throws IOException {
|
||||
boolean flagChange = false;
|
||||
if (path != null && path.length() != 0) {
|
||||
String[] dirs = path.split("/");
|
||||
ftpClient.changeWorkingDirectory("/");
|
||||
|
||||
for (int i = 1; dirs != null && i < dirs.length; i++) {
|
||||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||||
flagChange = ftpClient.changeWorkingDirectory(dirs[i]);
|
||||
}
|
||||
}
|
||||
// 该语句必须位于创建目录之后
|
||||
System.out.println("【目录切换】" + path + flagChange);
|
||||
try {
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
FTPFile[] file = ftpClient.listFiles(fileName);
|
||||
return file.length > 0 ? true : false;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到文件列表,listFiles返回包含目录和文件,它返回的是一个FTPFile数组
|
||||
* listNames():只包含目录的字符串数组
|
||||
@@ -308,6 +338,13 @@ public class FtpOperationUtil {
|
||||
return ftpClient.deleteFile(pathName);
|
||||
}
|
||||
|
||||
public boolean renameFile(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");
|
||||
|
||||
return ftpClient.rename(oldFileName, newFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到ftp服务器
|
||||
* 在进行上传和下载文件的时候,设置文件的类型最好是:
|
||||
|
||||
Reference in New Issue
Block a user