2018-01-17 14:10:40 +08:00
|
|
|
|
package cn.keking.web.controller;
|
|
|
|
|
|
2019-04-16 21:09:32 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2022-07-25 17:26:02 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2022-07-25 18:33:22 +08:00
|
|
|
|
import cn.keking.utils.KkFileUtils;
|
2023-01-17 22:56:51 +08:00
|
|
|
|
import cn.keking.utils.WebUtils;
|
2020-05-14 10:04:09 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2023-03-29 15:51:17 +08:00
|
|
|
|
import org.springframework.util.ObjectUtils;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import org.springframework.util.StreamUtils;
|
2022-07-25 17:26:02 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2022-12-14 09:40:37 +08:00
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
2023-03-29 15:51:17 +08:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author yudian-it
|
2023-01-11 13:26:18 +08:00
|
|
|
|
* 2017/12/1
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
public class FileController {
|
2020-05-14 10:04:09 +08:00
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(FileController.class);
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
|
|
|
|
private final String demoDir = "demo";
|
|
|
|
|
private final String demoPath = demoDir + File.separator;
|
2023-01-17 22:56:51 +08:00
|
|
|
|
public static final String BASE64_DECODE_ERROR_MSG = "Base64解码失败,请检查你的 %s 是否采用 Base64 + urlEncode 双重编码了!";
|
2023-03-29 15:51:17 +08:00
|
|
|
|
private static final String[] not_allowed = { "dll", "exe", "msi" }; // 不允许上传的文件扩展名
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@PostMapping("/fileUpload")
|
2022-12-14 09:40:37 +08:00
|
|
|
|
public ReturnResponse<Object> fileUpload(@RequestParam("file") MultipartFile file) {
|
2023-03-29 15:51:17 +08:00
|
|
|
|
ReturnResponse<Object> checkResult = this.fileUploadCheck(file);
|
|
|
|
|
if (checkResult.isFailure()) {
|
|
|
|
|
return checkResult;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
File outFile = new File(fileDir + demoPath);
|
2020-12-27 01:43:50 +08:00
|
|
|
|
if (!outFile.exists() && !outFile.mkdirs()) {
|
2022-07-25 17:26:02 +08:00
|
|
|
|
logger.error("创建文件夹【{}】失败,请检查目录权限!", fileDir + demoPath);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2023-03-29 15:51:17 +08:00
|
|
|
|
String fileName = checkResult.getContent().toString();
|
|
|
|
|
logger.info("上传文件:{}{}{}", fileDir, demoPath, fileName);
|
2022-12-14 09:40:37 +08:00
|
|
|
|
try (InputStream in = file.getInputStream(); OutputStream out = Files.newOutputStream(Paths.get(fileDir + demoPath + fileName))) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
StreamUtils.copy(in, out);
|
2022-07-25 18:33:22 +08:00
|
|
|
|
return ReturnResponse.success(null);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
} catch (IOException e) {
|
2020-05-14 10:04:09 +08:00
|
|
|
|
logger.error("文件上传失败", e);
|
2022-07-25 18:33:22 +08:00
|
|
|
|
return ReturnResponse.failure();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@GetMapping("/deleteFile")
|
2022-12-14 09:40:37 +08:00
|
|
|
|
public ReturnResponse<Object> deleteFile(String fileName) {
|
2023-03-29 15:51:17 +08:00
|
|
|
|
ReturnResponse<Object> checkResult = this.deleteFileCheck(fileName);
|
|
|
|
|
if (checkResult.isFailure()) {
|
|
|
|
|
return checkResult;
|
2022-07-25 18:33:22 +08:00
|
|
|
|
}
|
2023-03-29 15:51:17 +08:00
|
|
|
|
fileName = checkResult.getContent().toString();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
File file = new File(fileDir + demoPath + fileName);
|
2020-05-14 10:04:09 +08:00
|
|
|
|
logger.info("删除文件:{}", file.getAbsolutePath());
|
2020-12-27 01:43:50 +08:00
|
|
|
|
if (file.exists() && !file.delete()) {
|
2022-07-25 18:33:22 +08:00
|
|
|
|
String msg = String.format("删除文件【%s】失败,请检查目录权限!", file.getPath());
|
|
|
|
|
logger.error(msg);
|
|
|
|
|
return ReturnResponse.failure(msg);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2022-07-25 18:33:22 +08:00
|
|
|
|
return ReturnResponse.success();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@GetMapping("/listFiles")
|
2022-12-14 09:40:37 +08:00
|
|
|
|
public List<Map<String, String>> getFiles() {
|
2020-12-26 01:52:52 +08:00
|
|
|
|
List<Map<String, String>> list = new ArrayList<>();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
File file = new File(fileDir + demoPath);
|
|
|
|
|
if (file.exists()) {
|
2023-03-24 13:41:19 +08:00
|
|
|
|
File[] files = Objects.requireNonNull(file.listFiles());
|
|
|
|
|
Arrays.sort(files, (f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified()));
|
|
|
|
|
Arrays.stream(files).forEach(file1 -> {
|
2020-12-27 01:43:50 +08:00
|
|
|
|
Map<String, String> fileName = new HashMap<>();
|
2020-12-26 01:52:52 +08:00
|
|
|
|
fileName.put("fileName", demoDir + "/" + file1.getName());
|
|
|
|
|
list.add(fileName);
|
|
|
|
|
});
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2022-07-25 18:33:22 +08:00
|
|
|
|
return list;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 15:51:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* 上传文件前校验
|
|
|
|
|
*
|
|
|
|
|
* @param file 文件
|
|
|
|
|
* @return 校验结果
|
|
|
|
|
*/
|
|
|
|
|
private ReturnResponse<Object> fileUploadCheck(MultipartFile file) {
|
|
|
|
|
if (ConfigConstants.getFileUploadDisable()) {
|
|
|
|
|
return ReturnResponse.failure("文件传接口已禁用");
|
|
|
|
|
}
|
|
|
|
|
String fileName = WebUtils.getFileNameFromMultipartFile(file);
|
|
|
|
|
|
|
|
|
|
if (!isAllowedUpload(fileName)) {
|
|
|
|
|
return ReturnResponse.failure("不允许上传的文件类型: " + fileName);
|
|
|
|
|
}
|
|
|
|
|
if (KkFileUtils.isIllegalFileName(fileName)) {
|
|
|
|
|
return ReturnResponse.failure("不允许上传的文件名: " + fileName);
|
|
|
|
|
}
|
|
|
|
|
// 判断是否存在同名文件
|
|
|
|
|
if (existsFile(fileName)) {
|
|
|
|
|
return ReturnResponse.failure("存在同名文件,请先删除原有文件再次上传");
|
|
|
|
|
}
|
|
|
|
|
return ReturnResponse.success(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断文件是否允许上传
|
|
|
|
|
*
|
|
|
|
|
* @param file 文件扩展名
|
|
|
|
|
* @return 是否允许上传
|
|
|
|
|
*/
|
|
|
|
|
private boolean isAllowedUpload(String file) {
|
|
|
|
|
String fileType = KkFileUtils.suffixFromFileName(file);
|
|
|
|
|
for (String type : not_allowed) {
|
|
|
|
|
if (type.equals(fileType))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return !ObjectUtils.isEmpty(fileType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除文件前校验
|
|
|
|
|
*
|
|
|
|
|
* @param fileName 文件名
|
|
|
|
|
* @return 校验结果
|
|
|
|
|
*/
|
|
|
|
|
private ReturnResponse<Object> deleteFileCheck(String fileName) {
|
|
|
|
|
if (ObjectUtils.isEmpty(fileName)) {
|
|
|
|
|
return ReturnResponse.failure("文件名为空,删除失败!");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
fileName = WebUtils.decodeUrl(fileName);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, fileName);
|
|
|
|
|
return ReturnResponse.failure(errorMsg + "删除失败!");
|
|
|
|
|
}
|
|
|
|
|
assert fileName != null;
|
|
|
|
|
if (fileName.contains("/")) {
|
|
|
|
|
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
|
|
|
|
}
|
|
|
|
|
if (KkFileUtils.isIllegalFileName(fileName)) {
|
|
|
|
|
return ReturnResponse.failure("非法文件名,删除失败!");
|
|
|
|
|
}
|
|
|
|
|
return ReturnResponse.success(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 10:04:09 +08:00
|
|
|
|
private boolean existsFile(String fileName) {
|
|
|
|
|
File file = new File(fileDir + demoPath + fileName);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
return file.exists();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|