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.exception.UserException;
|
||||||
import com.fjy.spring.service.FileService;
|
import com.fjy.spring.service.FileService;
|
||||||
import com.fjy.spring.service.LogService;
|
import com.fjy.spring.service.LogService;
|
||||||
|
import com.fjy.spring.untils.FtpOperationUtil;
|
||||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@@ -47,6 +49,7 @@ public class DeleteController {
|
|||||||
TbFile resfile = fileService.findFileById(tbFile);
|
TbFile resfile = fileService.findFileById(tbFile);
|
||||||
return resfile.getColfilepath();
|
return resfile.getColfilepath();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件,可以是文件或文件夹
|
* 删除文件,可以是文件或文件夹
|
||||||
*/
|
*/
|
||||||
@@ -62,9 +65,9 @@ public class DeleteController {
|
|||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (filepath.isFile()) {
|
if (filepath.isFile()) {
|
||||||
return deleteFile(resfile.getColfilepath(),resfile.getColfileid());
|
return deleteFTPFile(resfile);
|
||||||
}
|
// return deleteFile(resfile.getColfilepath(), resfile.getColfileid());
|
||||||
else{
|
} else {
|
||||||
return deleteDirectory(resfile.getColfilepath());
|
return deleteDirectory(resfile.getColfilepath());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,8 +83,23 @@ public class DeleteController {
|
|||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
TbFile tbFile = new TbFile();
|
TbFile tbFile = new TbFile();
|
||||||
tbFile.setColfileid(fileid);
|
tbFile.setColfileid(fileid);
|
||||||
|
|
||||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||||
if (file.exists() && file.isFile()) {
|
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()) {
|
if (file.delete()) {
|
||||||
fileService.deleteFileById(tbFile);
|
fileService.deleteFileById(tbFile);
|
||||||
log.info("删除单个文件" + fileName + "成功!");
|
log.info("删除单个文件" + fileName + "成功!");
|
||||||
@@ -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
|
* @return 目录删除成功返回true,否则返回false
|
||||||
*/
|
*/
|
||||||
public boolean deleteDirectory(String path) {
|
public boolean deleteDirectory(String path) {
|
||||||
|
|||||||
@@ -267,7 +267,35 @@ public class UpLoadController {
|
|||||||
ftpOperationUtil.connectServer();
|
ftpOperationUtil.connectServer();
|
||||||
InputStream inputStream = file.getInputStream();
|
InputStream inputStream = file.getInputStream();
|
||||||
String path = "/upload/" + courseName + "/" + folder + "/";
|
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,
|
boolean flag = ftpOperationUtil.uploadFile(inputStream,
|
||||||
tbFile.getColfilename(),path);
|
tbFile.getColfilename(),path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -283,7 +311,6 @@ public class UpLoadController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
file.transferTo(targetFile);
|
file.transferTo(targetFile);
|
||||||
// boolean flag = ftpOperationUtil.uploadFile(tbFile.getColfilepath(),tbFile.getColfilename());
|
|
||||||
fileList.add(
|
fileList.add(
|
||||||
request.getScheme() + "://" + request.getServerName() + ":"
|
request.getScheme() + "://" + request.getServerName() + ":"
|
||||||
+ serverProperties.getPortNum() + request.getContextPath() + "/upload/"
|
+ serverProperties.getPortNum() + request.getContextPath() + "/upload/"
|
||||||
|
|||||||
@@ -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数组
|
* 得到文件列表,listFiles返回包含目录和文件,它返回的是一个FTPFile数组
|
||||||
* listNames():只包含目录的字符串数组
|
* listNames():只包含目录的字符串数组
|
||||||
@@ -308,6 +338,13 @@ public class FtpOperationUtil {
|
|||||||
return ftpClient.deleteFile(pathName);
|
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服务器
|
* 上传文件到ftp服务器
|
||||||
* 在进行上传和下载文件的时候,设置文件的类型最好是:
|
* 在进行上传和下载文件的时候,设置文件的类型最好是:
|
||||||
|
|||||||
Reference in New Issue
Block a user