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;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-05-14 10:04:09 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
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 org.springframework.web.util.HtmlUtils;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2021-01-23 13:12:31 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-07-25 17:26:02 +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
|
|
|
|
|
* @date 2017/12/1
|
|
|
|
|
*/
|
|
|
|
|
@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;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@PostMapping("/fileUpload")
|
2020-05-14 10:04:09 +08:00
|
|
|
|
public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException {
|
2021-07-06 09:09:19 +08:00
|
|
|
|
if (ConfigConstants.getFileUploadDisable()) {
|
|
|
|
|
return new ObjectMapper().writeValueAsString(ReturnResponse.failure("文件传接口已禁用"));
|
|
|
|
|
}
|
2019-07-30 10:02:36 +08:00
|
|
|
|
// 获取文件名
|
2018-01-17 14:10:40 +08:00
|
|
|
|
String fileName = file.getOriginalFilename();
|
2019-07-30 10:02:36 +08:00
|
|
|
|
//判断是否为IE浏览器的文件名,IE浏览器下文件名会带有盘符信息
|
2022-07-25 17:26:02 +08:00
|
|
|
|
|
2021-01-16 04:32:41 +08:00
|
|
|
|
// escaping dangerous characters to prevent XSS
|
2022-07-25 17:26:02 +08:00
|
|
|
|
assert fileName != null;
|
2021-01-23 13:12:31 +08:00
|
|
|
|
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
|
2020-05-15 18:09:19 +08:00
|
|
|
|
int pos = (Math.max(winSep, unixSep));
|
2022-07-25 17:26:02 +08:00
|
|
|
|
if (pos != -1) {
|
2019-07-30 10:02:36 +08:00
|
|
|
|
fileName = fileName.substring(pos + 1);
|
|
|
|
|
}
|
2020-05-14 10:04:09 +08:00
|
|
|
|
// 判断是否存在同名文件
|
|
|
|
|
if (existsFile(fileName)) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return new ObjectMapper().writeValueAsString(ReturnResponse.failure("存在同名文件,请先删除原有文件再次上传"));
|
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
|
|
|
|
}
|
2020-05-18 09:46:52 +08:00
|
|
|
|
logger.info("上传文件:{}", fileDir + demoPath + fileName);
|
2022-07-25 17:26:02 +08:00
|
|
|
|
try (InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
StreamUtils.copy(in, out);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return new ObjectMapper().writeValueAsString(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);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return new ObjectMapper().writeValueAsString(ReturnResponse.failure());
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@GetMapping("/deleteFile")
|
2018-01-17 14:10:40 +08:00
|
|
|
|
public String deleteFile(String fileName) throws JsonProcessingException {
|
|
|
|
|
if (fileName.contains("/")) {
|
|
|
|
|
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
|
|
|
|
}
|
|
|
|
|
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 17:26:02 +08:00
|
|
|
|
logger.error("删除文件【{}】失败,请检查目录权限!", file.getPath());
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return new ObjectMapper().writeValueAsString(ReturnResponse.success());
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
@GetMapping("/listFiles")
|
2018-01-17 14:10:40 +08:00
|
|
|
|
public String getFiles() throws JsonProcessingException {
|
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()) {
|
2020-12-26 01:52:52 +08:00
|
|
|
|
Arrays.stream(Objects.requireNonNull(file.listFiles())).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
|
|
|
|
}
|
|
|
|
|
return new ObjectMapper().writeValueAsString(list);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|