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