实现单文件删除
This commit is contained in:
140
src/main/java/com/fjy/spring/controller/DeleteController.java
Normal file
140
src/main/java/com/fjy/spring/controller/DeleteController.java
Normal file
@@ -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<tmp.length;i++){
|
||||||
|
if(tmp[i].isDirectory()){
|
||||||
|
deleteDirectory(path+"/"+tmp[i].getName());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
tmp[i].delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dir.delete();
|
||||||
|
}*/
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,9 @@ package com.fjy.spring.repository;
|
|||||||
import com.fjy.spring.domain.TbFile;
|
import com.fjy.spring.domain.TbFile;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface TbFileRepository extends JpaRepository<TbFile,Integer>{
|
public interface TbFileRepository extends JpaRepository<TbFile,Integer>{
|
||||||
public Optional<TbFile> findByColfilename(String name);
|
public List<TbFile> findByColfilename(String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ public class FileService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TbFile findFile(TbFile tbFile){
|
public List<TbFile> findFile(TbFile tbFile){
|
||||||
TbFile file = (TbFile) tbFileRepository.findByColfilename(tbFile.getColfilename()).get();
|
List<TbFile> files = tbFileRepository.findByColfilename(tbFile.getColfilename());
|
||||||
if (file!=null)
|
if (files!=null)
|
||||||
return file;
|
return files;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,10 +30,17 @@ public class FileService {
|
|||||||
return tbFileRepository.findAll();
|
return tbFileRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TbFile findFileById(TbFile tbFile){
|
public TbFile findFileById(TbFile tbFile)
|
||||||
|
{
|
||||||
return tbFileRepository.findById(tbFile.getColfileid()).get();
|
return tbFileRepository.findById(tbFile.getColfileid()).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteFile(TbFile file){
|
||||||
|
tbFileRepository.delete(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFileById(TbFile file){
|
||||||
|
tbFileRepository.deleteById(file.getColfileid());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ var Main = {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
ClickToJump(targe){
|
ClickToJump(targe){
|
||||||
window.location.href="http://localhost:8080/cms/" + targe;
|
window.location.href="http://localhost:8080/cms/home/" + targe;
|
||||||
},
|
},
|
||||||
handleSelect(key, keyPath) {
|
handleSelect(key, keyPath) {
|
||||||
console.log(key, keyPath);
|
console.log(key, keyPath);
|
||||||
|
|||||||
@@ -108,7 +108,7 @@
|
|||||||
</el-row>
|
</el-row>
|
||||||
</div>
|
</div>
|
||||||
<script src="js/loginstyle.js"></script>
|
<script src="js/loginstyle.js"></script>
|
||||||
<script src="js/msg.js"></script>
|
<script src="js/common.js"></script>
|
||||||
<!--<body>
|
<!--<body>
|
||||||
<!–<center>
|
<!–<center>
|
||||||
<h1>登录系统</h1>
|
<h1>登录系统</h1>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script th:src="@{js/homePage.js}"></script>
|
<script th:src="@{/js/homePage.js}"></script>
|
||||||
<script th:src="@{js/msg.js}"></script>
|
<script th:src="@{/js/common.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -90,7 +90,7 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script th:src="@{../js/admin.js}"></script>
|
<script th:src="@{/js/admin.js}"></script>
|
||||||
<script th:src="@{../js/msg.js}"></script>
|
<script th:src="@{/js/common.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script th:src="@{js/homePage.js}"></script>
|
<script th:src="@{/js/homePage.js}"></script>
|
||||||
<script th:src="@{js/msg.js}"></script>
|
<script th:src="@{/js/common.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -304,7 +304,7 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script th:src="@{js/homePage.js}"></script>
|
<script th:src="@{/js/homePage.js}"></script>
|
||||||
<script th:src="@{js/msg.js}"></script>
|
<script th:src="@{/js/common.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -318,6 +318,6 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script src="../../static/js/homePage.js"></script>
|
<script src="../../static/js/homePage.js"></script>
|
||||||
<script src="../../static/js/msg.js"></script>
|
<script src="../../static/js/common.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -124,6 +124,6 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script src="../../static/js/homework.js"></script>
|
<script src="../../static/js/homework.js"></script>
|
||||||
<script src="../../static/js/msg.js"></script>
|
<script src="../../static/js/common.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -81,6 +81,6 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script th:src="@{/js/managecourse.js}"></script>
|
<script th:src="@{/js/managecourse.js}"></script>
|
||||||
<script th:src="@{/js/msg.js}"></script>
|
<script th:src="@{/js/common.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -96,6 +96,6 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script src="../../static/js/manageuser.js"></script>
|
<script src="../../static/js/manageuser.js"></script>
|
||||||
<script src="../../static/js/msg.js"></script>
|
<script src="../../static/js/common.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -149,6 +149,6 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<script src="../../static/js/user.js"></script>
|
<script src="../../static/js/user.js"></script>
|
||||||
<script src="../../static/js/msg.js"></script>
|
<script src="../../static/js/common.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user