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();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user