前端vue代码
<template>
<el-button type="primary" @click="deleteAll">删除</el-button>
</template>
<script setup>
import { ref } from 'vue';
import axios from 'axios';
const deleteAll = () => {
let fileNameList = ['file1.txt', 'file2.jpg', 'file3.pdf'];
fullLoading.show();
axios.get('file/deleteFile/' + fileNameList)
.then(response => {
if (response.status === 200) {
console.log("成功");
}
fullLoading.hide();
})
.catch(error => {
fullLoading.hide();
console.log(error);
})
}
</script>
Java代码
package cn.com.techfly.lzpid.system.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/file")
public class FileController {
private String FILE_PATH = "F:/upload";
@GetMapping(value = "/deleteFile/{fileNameList}",
produces = "application/json")
public ResponseEntity<Object> deleteSysManual(@PathVariable List<String> fileNameList) {
List<String> errors = new ArrayList<>();
for (String fileName : fileNameList) {
Path filePath = Paths.get(FILE_PATH, fileName);
try {
if (Files.exists(filePath)) {
Files.delete(filePath);
} else {
errors.add("文件不存在: " + fileName);
}
} catch (IOException e) {
errors.add("删除文件错误: " + fileName + " - " + e.getMessage());
}
}
if (errors.isEmpty()) {
return ResponseEntity.ok("");
} else {
return ResponseEntity.badRequest().body(errors);
}
}
}