From c66b0bd9aee46eb8fcf8341a3cf6595605d3b7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Fri, 9 Feb 2018 15:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8D=95=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/controller/DeleteController.java | 140 ++++++++++++++++++ .../spring/repository/TbFileRepository.java | 3 +- .../com/fjy/spring/service/FileService.java | 17 ++- src/main/resources/static/js/admin.js | 2 +- .../resources/static/js/{msg.js => common.js} | 0 src/main/resources/static/loginTest.html | 2 +- src/main/resources/templates/home/about.html | 4 +- src/main/resources/templates/home/admin.html | 4 +- .../resources/templates/home/feedback.html | 4 +- src/main/resources/templates/home/home.html | 4 +- src/main/resources/templates/home/home2.html | 2 +- .../resources/templates/home/homework.html | 2 +- .../templates/home/managecourse.html | 2 +- .../resources/templates/home/manageuser.html | 2 +- src/main/resources/templates/home/user.html | 2 +- 15 files changed, 169 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/fjy/spring/controller/DeleteController.java rename src/main/resources/static/js/{msg.js => common.js} (100%) diff --git a/src/main/java/com/fjy/spring/controller/DeleteController.java b/src/main/java/com/fjy/spring/controller/DeleteController.java new file mode 100644 index 0000000..603b97c --- /dev/null +++ b/src/main/java/com/fjy/spring/controller/DeleteController.java @@ -0,0 +1,140 @@ +package com.fjy.spring.controller; + +import com.fjy.spring.domain.TbFile; +import com.fjy.spring.enums.ResultEnum; +import com.fjy.spring.exception.UserException; +import com.fjy.spring.service.FileService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.io.File; + +@Controller +public class DeleteController { + @Autowired + private FileService fileService;//文件相关数据库操作 + + @GetMapping("/home/findfile") + @ResponseBody + public String findFilePath(@RequestParam(value = "fileid") Integer fileid){ + TbFile tbFile = new TbFile(); + tbFile.setColfileid(fileid); + TbFile resfile = fileService.findFileById(tbFile); + return resfile.getColfilepath(); + } + /** + * 删除文件,可以是文件或文件夹 + */ + @RequestMapping("/home/filedelete") + @ResponseBody + public void delete(@RequestParam(value = "fileid") Integer fileid)throws Exception { + TbFile tbFile = new TbFile(); + tbFile.setColfileid(fileid); + TbFile resfile = fileService.findFileById(tbFile); + File filepath = new File(resfile.getColfilepath()); + if (!filepath.exists()) { + System.out.println("删除文件失败:" + resfile.getColfilename() + "不存在!"); + } else { + if (filepath.isFile()){ + deleteFile(resfile.getColfilepath(),resfile.getColfileid()); + new UserException(ResultEnum.SUCCESS); + } + else + deleteDirectory(resfile.getColfilepath()); + } + + } + + /** + * 删除单个文件 + */ + public boolean deleteFile(String fileName,Integer fileid) { + File file = new File(fileName); + TbFile tbFile = new TbFile(); + tbFile.setColfileid(fileid); + // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 + if (file.exists() && file.isFile()) { + if (file.delete()) { + fileService.deleteFileById(tbFile); + System.out.println("删除单个文件" + fileName + "成功!"); + return true; + } else { + System.out.println("删除单个文件" + fileName + "失败!"); + return false; + } + } else { + System.out.println("删除单个文件失败:" + fileName + "不存在!"); + return false; + } + } + + /** + * 删除目录及目录下的文件 + *功能暂时不开放 + * @param path + * 要删除的目录的文件路径 + * @return 目录删除成功返回true,否则返回false + */ + public boolean deleteDirectory(String path) { + /*// 如果dir不以文件分隔符结尾,自动添加文件分隔符 + if (!dir.endsWith(File.separator)) + dir = dir + File.separator; + File dirFile = new File(dir); + // 如果dir对应的文件不存在,或者不是一个目录,则退出 + if ((!dirFile.exists()) || (!dirFile.isDirectory())) { + System.out.println("删除目录失败:" + dir + "不存在!"); + return false; + } + boolean flag = true; + // 删除文件夹中的所有文件包括子目录 + File[] files = dirFile.listFiles(); + for (int i = 0; i < files.length; i++) { + // 删除子文件 + if (files[i].isFile()) { + TbFile tbFile = new TbFile(); + tbFile.setColfilename(files[i].getName()); + fileService.deleteFileById(tbFile); + flag = deleteFile(files[i].getAbsolutePath()); + if (!flag) + break; + } + // 删除子目录 + else if (files[i].isDirectory()) { + flag = deleteDirectory(files[i] + .getAbsolutePath()); + if (!flag) + break; + } + } + if (!flag) { + System.out.println("删除目录失败!"); + return false; + } + // 删除当前目录 + if (dirFile.delete()) { + System.out.println("删除目录" + dir + "成功!"); + return true; + } else { + return false; + }*/ + + /*File dir=new File(path); + if(dir.exists()){ + File[] tmp=dir.listFiles(); + for(int i=0;i{ - public Optional findByColfilename(String name); + public List findByColfilename(String name); } diff --git a/src/main/java/com/fjy/spring/service/FileService.java b/src/main/java/com/fjy/spring/service/FileService.java index 0105a28..e78b941 100644 --- a/src/main/java/com/fjy/spring/service/FileService.java +++ b/src/main/java/com/fjy/spring/service/FileService.java @@ -19,10 +19,10 @@ public class FileService { return false; } - public TbFile findFile(TbFile tbFile){ - TbFile file = (TbFile) tbFileRepository.findByColfilename(tbFile.getColfilename()).get(); - if (file!=null) - return file; + public List findFile(TbFile tbFile){ + List files = tbFileRepository.findByColfilename(tbFile.getColfilename()); + if (files!=null) + return files; return null; } @@ -30,10 +30,17 @@ public class FileService { return tbFileRepository.findAll(); } - public TbFile findFileById(TbFile tbFile){ + public TbFile findFileById(TbFile tbFile) + { return tbFileRepository.findById(tbFile.getColfileid()).get(); } + public void deleteFile(TbFile file){ + tbFileRepository.delete(file); + } + public void deleteFileById(TbFile file){ + tbFileRepository.deleteById(file.getColfileid()); + } } diff --git a/src/main/resources/static/js/admin.js b/src/main/resources/static/js/admin.js index 32020b3..bd70ab0 100644 --- a/src/main/resources/static/js/admin.js +++ b/src/main/resources/static/js/admin.js @@ -61,7 +61,7 @@ var Main = { }, methods: { ClickToJump(targe){ - window.location.href="http://localhost:8080/cms/" + targe; + window.location.href="http://localhost:8080/cms/home/" + targe; }, handleSelect(key, keyPath) { console.log(key, keyPath); diff --git a/src/main/resources/static/js/msg.js b/src/main/resources/static/js/common.js similarity index 100% rename from src/main/resources/static/js/msg.js rename to src/main/resources/static/js/common.js diff --git a/src/main/resources/static/loginTest.html b/src/main/resources/static/loginTest.html index 59ec417..dcca858 100644 --- a/src/main/resources/static/loginTest.html +++ b/src/main/resources/static/loginTest.html @@ -108,7 +108,7 @@ - +