file-online-preview/server/src/main/java/cn/keking/web/controller/FileController.java

136 lines
5.4 KiB
Java
Raw Normal View History

package cn.keking.web.controller;
import cn.keking.config.ConfigConstants;
import cn.keking.model.ReturnResponse;
2022-07-25 18:33:22 +08:00
import cn.keking.utils.KkFileUtils;
import cn.keking.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
2022-12-14 09:40:37 +08:00
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
* @author yudian-it
* 2017/12/1
*/
@RestController
public class FileController {
private final Logger logger = LoggerFactory.getLogger(FileController.class);
private final String fileDir = ConfigConstants.getFileDir();
private final String demoDir = "demo";
private final String demoPath = demoDir + File.separator;
public static final String BASE64_DECODE_ERROR_MSG = "Base64解码失败请检查你的 %s 是否采用 Base64 + urlEncode 双重编码了!";
@PostMapping("/fileUpload")
2022-12-14 09:40:37 +08:00
public ReturnResponse<Object> fileUpload(@RequestParam("file") MultipartFile file) {
if (ConfigConstants.getFileUploadDisable()) {
2022-07-25 18:33:22 +08:00
return ReturnResponse.failure("文件传接口已禁用");
}
2019-07-30 10:02:36 +08:00
// 获取文件名
String fileName = file.getOriginalFilename();
2019-07-30 10:02:36 +08:00
//判断是否为IE浏览器的文件名IE浏览器下文件名会带有盘符信息
2021-01-16 04:32:41 +08:00
// escaping dangerous characters to prevent XSS
assert fileName != null;
fileName = HtmlUtils.htmlEscape(fileName, StandardCharsets.UTF_8.name());
2019-07-30 10:02:36 +08:00
// Check for Unix-style path
int unixSep = fileName.lastIndexOf('/');
// Check for Windows-style path
int winSep = fileName.lastIndexOf('\\');
// Cut off at latest possible point
int pos = (Math.max(winSep, unixSep));
if (pos != -1) {
2019-07-30 10:02:36 +08:00
fileName = fileName.substring(pos + 1);
}
String fileType= "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
fileType= fileName.substring(i+1);
fileType= fileType.toLowerCase();
}
if (fileType.length() == 0 || fileType.equals("dll") || fileType.equals("exe") || fileType.equals("msi") ){
return ReturnResponse.failure(fileName+"不允许上传的文件");
}
// 判断是否存在同名文件
if (existsFile(fileName)) {
2022-07-25 18:33:22 +08:00
return ReturnResponse.failure("存在同名文件,请先删除原有文件再次上传");
}
File outFile = new File(fileDir + demoPath);
if (!outFile.exists() && !outFile.mkdirs()) {
logger.error("创建文件夹【{}】失败,请检查目录权限!", fileDir + demoPath);
}
2020-05-18 09:46:52 +08:00
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))) {
StreamUtils.copy(in, out);
in.close();
out.close();
2022-07-25 18:33:22 +08:00
return ReturnResponse.success(null);
} catch (IOException e) {
logger.error("文件上传失败", e);
2022-07-25 18:33:22 +08:00
return ReturnResponse.failure();
}
}
@GetMapping("/deleteFile")
2022-12-14 09:40:37 +08:00
public ReturnResponse<Object> deleteFile(String fileName) {
if (fileName == null || fileName.length() == 0) {
return ReturnResponse.failure("文件名为空,删除失败!");
}
try {
fileName = WebUtils.decodeUrl(fileName);
} catch (Exception ex) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
return ReturnResponse.failure(errorMsg+"删除失败!");
2022-12-14 09:40:37 +08:00
}
if (fileName.contains("/")) {
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
}
2022-07-25 18:33:22 +08:00
if (KkFileUtils.isIllegalFileName(fileName)) {
return ReturnResponse.failure("非法文件名,删除失败!");
}
File file = new File(fileDir + demoPath + fileName);
logger.info("删除文件:{}", file.getAbsolutePath());
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);
}
2022-07-25 18:33:22 +08:00
return ReturnResponse.success();
}
@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<>();
File file = new File(fileDir + demoPath);
if (file.exists()) {
2020-12-26 01:52:52 +08:00
Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> {
Map<String, String> fileName = new HashMap<>();
2020-12-26 01:52:52 +08:00
fileName.put("fileName", demoDir + "/" + file1.getName());
list.add(fileName);
});
}
2022-07-25 18:33:22 +08:00
return list;
}
private boolean existsFile(String fileName) {
File file = new File(fileDir + demoPath + fileName);
return file.exists();
}
}