diff --git a/src/main/java/com/fjy/spring/controller/DeleteController.java b/src/main/java/com/fjy/spring/controller/DeleteController.java index 55eefbd..a39a32b 100644 --- a/src/main/java/com/fjy/spring/controller/DeleteController.java +++ b/src/main/java/com/fjy/spring/controller/DeleteController.java @@ -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; } } diff --git a/src/main/java/com/fjy/spring/controller/DownLoadController.java b/src/main/java/com/fjy/spring/controller/DownLoadController.java index 4c76377..631e2a6 100644 --- a/src/main/java/com/fjy/spring/controller/DownLoadController.java +++ b/src/main/java/com/fjy/spring/controller/DownLoadController.java @@ -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]; diff --git a/src/main/java/com/fjy/spring/controller/UpLoadController.java b/src/main/java/com/fjy/spring/controller/UpLoadController.java index c3f53db..eda9dd3 100644 --- a/src/main/java/com/fjy/spring/controller/UpLoadController.java +++ b/src/main/java/com/fjy/spring/controller/UpLoadController.java @@ -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); diff --git a/src/main/java/com/fjy/spring/untils/FtpOperationUtil.java b/src/main/java/com/fjy/spring/untils/FtpOperationUtil.java index d7b8853..52f951c 100644 --- a/src/main/java/com/fjy/spring/untils/FtpOperationUtil.java +++ b/src/main/java/com/fjy/spring/untils/FtpOperationUtil.java @@ -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(); diff --git a/src/test/java/com/fjy/spring/untils/FtpOperationUtilTest.java b/src/test/java/com/fjy/spring/untils/FtpOperationUtilTest.java index ace2ef0..82526ca 100644 --- a/src/test/java/com/fjy/spring/untils/FtpOperationUtilTest.java +++ b/src/test/java/com/fjy/spring/untils/FtpOperationUtilTest.java @@ -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); }