FTP所有操作集成成功
This commit is contained in:
@@ -60,19 +60,10 @@ public class DeleteController {
|
||||
tbFile.setColfileid(fileid);
|
||||
TbFile resfile = fileService.findFileById(tbFile);
|
||||
File filepath = new File(resfile.getColfilepath());
|
||||
if (!filepath.exists()) {
|
||||
log.error("删除文件失败:" + resfile.getColfilename() + "不存在!");
|
||||
return false;
|
||||
} else {
|
||||
if (filepath.isFile()) {
|
||||
return deleteFTPFile(resfile);
|
||||
// return deleteFile(resfile.getColfilepath(), resfile.getColfileid());
|
||||
} else {
|
||||
return deleteDirectory(resfile.getColfilepath());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return deleteFTPFile(resfile);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -132,7 +123,7 @@ public class DeleteController {
|
||||
|
||||
public boolean deleteFTPFile(TbFile tbFile) {
|
||||
tbFile.setColfileid(tbFile.getColfileid());
|
||||
String path = "/upload/" + tbFile.getCourseName() + "/" + tbFile.getWorkFolder() + "/" + tbFile.getColfilename();
|
||||
String path = "/upload/" + tbFile.getCourseName() + "/" + tbFile.getWorkFolder() + "/";
|
||||
FtpOperationUtil ftpOperationUtil = new FtpOperationUtil();
|
||||
boolean flagExistsFile = false;
|
||||
boolean flagDeleteFile = false;
|
||||
@@ -146,15 +137,15 @@ public class DeleteController {
|
||||
|
||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||
if (flagExistsFile) {
|
||||
System.out.println("【path】" + path);
|
||||
if (flagExistsFile) {
|
||||
try {
|
||||
flagDeleteFile = ftpOperationUtil.deleteFile(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("【flagDeleteFile】" + flagDeleteFile);
|
||||
System.out.println("【path】" + path);
|
||||
if (flagExistsFile) {
|
||||
try {
|
||||
flagDeleteFile = ftpOperationUtil.deleteFile(path + tbFile.getColfilename());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("【flagDeleteFile】" + flagDeleteFile);
|
||||
}
|
||||
|
||||
if (flagDeleteFile) {
|
||||
fileService.deleteFileById(tbFile);
|
||||
@@ -181,7 +172,7 @@ public class DeleteController {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log.info("删除单个文件失败:" + path + "不存在!");
|
||||
log.info("删除单个文件失败:" + path + tbFile.getColfilename() + "不存在!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class DownLoadController {
|
||||
throw new UserException(ResultEnum.EMPTY_DATA);
|
||||
}
|
||||
|
||||
@RequestMapping("/home/download/dodownload")
|
||||
|
||||
public String download(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
@@ -143,7 +143,7 @@ public class DownLoadController {
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/home/download/dodownloadftp")
|
||||
@RequestMapping("/home/download/dodownload")
|
||||
public String downloadFromFTP(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) {
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
try {
|
||||
@@ -174,8 +174,10 @@ public class DownLoadController {
|
||||
for (String str : list) {
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
InputStream inputStream = ftpUtil.downFile(tbFile.getColfilename());
|
||||
String path = "/upload/" + tbFile.getCourseName() + "/" + tbFile.getWorkFolder() + "/";
|
||||
System.out.println("【path】"+path);
|
||||
InputStream inputStream = ftpUtil.downFile(path,tbFile.getColfilename());
|
||||
System.out.println("【available】"+inputStream.available());
|
||||
bis = new BufferedInputStream(inputStream);
|
||||
bos = new BufferedOutputStream(response.getOutputStream());
|
||||
byte[] buff = new byte[2048];
|
||||
|
||||
@@ -166,7 +166,7 @@ public class UpLoadController {
|
||||
TbUser user = (TbUser) request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss");
|
||||
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
String dateNowStr2 = sdf2.format(date);
|
||||
String uploadUrl;
|
||||
@@ -212,8 +212,8 @@ public class UpLoadController {
|
||||
}
|
||||
|
||||
File targetFile = new File(pathname);
|
||||
//若文件已存在则自动重命名
|
||||
if (targetFile.exists()) {
|
||||
|
||||
/*if (targetFile.exists()) {
|
||||
String bakpathname;
|
||||
if (rename) {
|
||||
bakpathname = uploadUrl + "bak/" + user.getColstudentno() + user.getColrealname() + suffix;
|
||||
@@ -236,7 +236,7 @@ public class UpLoadController {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
log.info("文件上传到: " + uploadUrl + filename);
|
||||
log.info("文件大小: " + FormatFileSizeUtil.GetFileSize(file.getSize()));
|
||||
@@ -269,6 +269,7 @@ public class UpLoadController {
|
||||
String path = "/upload/" + courseName + "/" + folder + "/";
|
||||
boolean flagExistsFile = ftpOperationUtil.isExistsFile(path,tbFile.getColfilename());
|
||||
System.out.println("flagExistsFile"+flagExistsFile);
|
||||
//若文件已存在则自动重命名
|
||||
if (flagExistsFile){
|
||||
String oldFileName;
|
||||
if (rename) {
|
||||
@@ -282,11 +283,11 @@ public class UpLoadController {
|
||||
|
||||
tbFile.setColfilepath(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
tbFile.setColfilename(tbFile.getColfilename() + "." + dateNowStr2 + ".bak");
|
||||
/*if (fileService.addFile(file1)) {
|
||||
if (fileService.addFile(tbFile)) {
|
||||
log.info("重命名文件数据库更新成功");
|
||||
} else {
|
||||
log.error("重命名文件数据库更新失败");
|
||||
}*/
|
||||
}
|
||||
File mvfile = new File(oldFileName + "." + dateNowStr2 + ".bak");
|
||||
try {
|
||||
// FileUtils.moveFile(targetFile, mvfile);
|
||||
|
||||
@@ -263,6 +263,7 @@ public class FtpOperationUtil {
|
||||
*/
|
||||
public boolean isExistsFile(String path, String fileName) throws IOException {
|
||||
boolean flagChange = false;
|
||||
|
||||
if (path != null && path.length() != 0) {
|
||||
String[] dirs = path.split("/");
|
||||
ftpClient.changeWorkingDirectory("/");
|
||||
@@ -277,8 +278,9 @@ public class FtpOperationUtil {
|
||||
try {
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
FTPFile[] file = ftpClient.listFiles(fileName);
|
||||
return file.length > 0 ? true : false;
|
||||
return file.length > 0;
|
||||
} catch (IOException e) {
|
||||
e.getMessage();
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
@@ -325,6 +327,16 @@ public class FtpOperationUtil {
|
||||
return ftpClient.listNames();
|
||||
}
|
||||
|
||||
public String[] list(String path) throws IOException {
|
||||
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");
|
||||
ftpClient.changeWorkingDirectory(dirs[i]);
|
||||
}
|
||||
return ftpClient.listNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除服务器上的文件
|
||||
*
|
||||
@@ -522,6 +534,16 @@ public class FtpOperationUtil {
|
||||
return ftpClient.retrieveFileStream(sourceFileName);
|
||||
}
|
||||
|
||||
public InputStream downFile(String path,String sourceFileName) throws IOException {
|
||||
sourceFileName = new String(sourceFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||||
if (path != null && path.length() != 0) {
|
||||
boolean flagChange = ftpClient.changeWorkingDirectory(path);
|
||||
System.out.println("【目录切换】" + flagChange);
|
||||
}
|
||||
return ftpClient.retrieveFileStream(sourceFileName);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//testUpload();
|
||||
//testDownload();
|
||||
|
||||
@@ -71,7 +71,7 @@ public class FtpOperationUtilTest {
|
||||
@Test
|
||||
public void isExistsFile() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
boolean flag = ftpUtil.isExistsFile(FILE_NAME);
|
||||
boolean flag = ftpUtil.isExistsFile("/信息安全/","15级15251101261FJY实验二.doc.2018_05_09_21_25_47.bak");
|
||||
System.out.println("isExistsFile:" + flag);
|
||||
listFiles(ftpUtil);
|
||||
}
|
||||
@@ -79,13 +79,13 @@ public class FtpOperationUtilTest {
|
||||
@Test
|
||||
public void getFileList() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
ftpUtil.getFileList("");
|
||||
ftpUtil.getFileList("/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void list() throws IOException {
|
||||
FtpOperationUtil ftpUtil = makeConnectionFactory();
|
||||
String[] lists = ftpUtil.list();
|
||||
String[] lists = ftpUtil.list("/upload/信息安全/时间测试/");
|
||||
for (String list : lists) {
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user