package com.se.simu.controller;
|
|
import com.se.simu.domain.vo.R;
|
import com.se.simu.utils.FileUtil;
|
import com.se.simu.utils.ZipUtils;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.List;
|
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.ExecutionException;
|
import java.util.zip.ZipEntry;
|
|
@Api(tags = "0-文件相关接口")
|
@Slf4j
|
@CrossOrigin(origins = "*")
|
@RestController
|
@RequestMapping("/v1/files")
|
public class FilesUploadController extends BaseController {
|
|
|
@Value("${simu-app.filePath}")
|
// private final String uploadedFolder = "D:/home/scheme/upload";
|
private String uploadedFolder;
|
|
|
@ApiOperation("0-文件上传地址")
|
@GetMapping("/getUploadPath")
|
public ResponseEntity<String> getUploadPath() throws Exception {
|
return ResponseEntity.ok("文件上传地址为:" + uploadedFolder);
|
}
|
|
|
@ApiOperation("1-上传单个文件")
|
@PostMapping("/upload")
|
public R<Object> upload(@RequestParam("file") MultipartFile file) throws IOException {
|
if (file.isEmpty()) {
|
return success("文件不能为空");
|
}
|
// 获取当前年月日
|
String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
String targetDir = Paths.get(uploadedFolder, date).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
try {
|
// 获取当前时分秒
|
String time = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmmss"));
|
// 文件地址全称
|
Path filePath = Paths.get(targetDir, time + "_" + file.getOriginalFilename());
|
// 文件名
|
file.transferTo(filePath);
|
return success(targetDir + "\\" + time + "_" + file.getOriginalFilename(), "文件上传成功");
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
return fail("文件上传失败");
|
}
|
}
|
|
@ApiOperation("1-上传单个shp文件")
|
@PostMapping("/uploadShp")
|
public R<Object> uploadShp(@RequestParam("file") MultipartFile file) throws IOException {
|
if (file.isEmpty()) {
|
return success("文件不能为空");
|
}
|
// 获取当前年月日
|
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
|
String targetDir = Paths.get(uploadedFolder, date).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
try {
|
// 文件地址全称
|
Path filePath = Paths.get(targetDir, file.getOriginalFilename());
|
// 文件名
|
file.transferTo(filePath);
|
ZipUtils.unzip(targetDir + "\\" + file.getOriginalFilename(),targetDir);
|
File zipfile=new File(targetDir + "\\" + file.getOriginalFilename());
|
zipfile.delete();
|
return success(FileUtil.getShpPath(targetDir), "文件上传成功");
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
return fail("文件上传失败");
|
}
|
}
|
|
@ApiOperation("1-上传单个文件")
|
@PostMapping("/uploads")
|
public ResponseEntity<String> uploads(@RequestParam("file") MultipartFile file) throws IOException {
|
if (file.isEmpty()) {
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
|
}
|
// 获取当前年月日
|
String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
String targetDir = Paths.get(uploadedFolder, date).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
try {
|
// 获取当前时分秒
|
String time = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmmss"));
|
// 文件地址全称
|
Path filePath = Paths.get(targetDir, time + "_" + file.getOriginalFilename());
|
// 文件名
|
file.transferTo(filePath);
|
return ResponseEntity.ok("文件上传成功");
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
|
}
|
}
|
|
@ApiOperation("2-0上传多个文件")
|
@PostMapping("/uploadFiles")
|
public ResponseEntity<String> uploadFiles(List<MultipartFile> files) throws IOException {
|
if (files.isEmpty()) {
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
|
}
|
// 上传文件路径
|
String targetDir = Paths.get(uploadedFolder, "upload").toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
CompletableFuture<Void> allUploadTasks = CompletableFuture.allOf(files.stream()
|
.map(file -> CompletableFuture.runAsync(() -> {
|
try {
|
file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
throw new RuntimeException("文件上传失败");
|
}
|
})).toArray(CompletableFuture[]::new));
|
try {
|
allUploadTasks.get();
|
return ResponseEntity.ok("所有文件上传成功!上传目录为:" + targetDir);
|
} catch (InterruptedException | ExecutionException e) {
|
log.error("文件上传失败", e);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
|
}
|
}
|
|
|
@ApiOperation("1-1 -上传单个文件")
|
@ApiImplicitParam(name = "filePathName", value = "文件夹名称", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class)
|
@PostMapping("/uploadSingleFile")
|
public ResponseEntity<String> uploadSingleFile(@RequestParam("file") MultipartFile file, @RequestParam("filePathName") String filePathName) throws IOException {
|
if (file.isEmpty()) {
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
|
}
|
// 判断 filePathName 是否为空
|
if (filePathName == null || filePathName.isEmpty()) {
|
filePathName = "upload";
|
//return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件夹名称不能为空");
|
}
|
String targetDir = Paths.get(uploadedFolder, filePathName).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
try {
|
file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
|
return ResponseEntity.ok("文件上传成功");
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
|
}
|
}
|
|
@ApiOperation("2-上传多个文件")
|
@ApiImplicitParam(name = "filePathName", value = "文件夹名称", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class)
|
@PostMapping("/uploadMultipleFiles")
|
public ResponseEntity<String> uploadMultipleFiles(List<MultipartFile> files, @RequestParam("filePathName") String filePathName) throws IOException {
|
if (files.isEmpty()) {
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
|
}
|
// 判断 filePathName 是否为空
|
if (filePathName == null || filePathName.isEmpty()) {
|
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件夹名称不能为空");
|
filePathName = "upload";
|
}
|
// 上传文件路径
|
String targetDir = Paths.get(uploadedFolder, filePathName).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
CompletableFuture<Void> allUploadTasks = CompletableFuture.allOf(files.stream()
|
.map(file -> CompletableFuture.runAsync(() -> {
|
try {
|
file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
throw new RuntimeException("文件上传失败");
|
}
|
})).toArray(CompletableFuture[]::new));
|
try {
|
allUploadTasks.get();
|
return ResponseEntity.ok("所有文件上传成功!上传目录为:" + targetDir);
|
} catch (InterruptedException | ExecutionException e) {
|
log.error("文件上传失败", e);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
|
}
|
}
|
|
|
@ApiOperation("2-1上传多个文件")
|
@ApiImplicitParam(name = "filePathName", value = "文件夹名称", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class)
|
@PostMapping("/uploadMultipleFile")
|
public ResponseEntity<String> uploadMultipleFile(List<MultipartFile> files, @RequestParam("filePathName") String filePathName) throws IOException {
|
// 上传文件路径
|
String targetDir = Paths.get(uploadedFolder, filePathName).toString();
|
log.info("目标目录: {}", targetDir);
|
createDirectoriesIfNotExists(targetDir);
|
try {
|
for (MultipartFile file : files) {
|
String fileName = file.getOriginalFilename();
|
File targetFile = new File(targetDir, fileName);
|
file.transferTo(targetFile);
|
}
|
return ResponseEntity.ok("所有文件上传成功!上传目录为:" + targetDir);
|
} catch (IOException e) {
|
log.error("文件上传失败", e);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
|
}
|
}
|
|
|
private File newFile(File destinationDir, ZipEntry zipEntry) throws Exception {
|
File destFile = new File(destinationDir, zipEntry.getName());
|
|
String destDirPath = destinationDir.getCanonicalPath();
|
String destFilePath = destFile.getCanonicalPath();
|
|
if (!destFilePath.startsWith(destDirPath + File.separator)) {
|
throw new Exception("Entry is outside of the target dir: " + zipEntry.getName());
|
}
|
|
return destFile;
|
}
|
|
|
/**
|
* 按文件路径删除文件夹
|
* 递归删除文件夹及文件的方法
|
*
|
* @param folder 文件夹
|
*/
|
private void deleteFolderByFilePath(File folder) {
|
if (folder.exists()) {
|
File[] files = folder.listFiles();
|
if (files != null) {
|
for (File file : files) {
|
if (file.isDirectory()) {
|
// 递归删除子文件夹
|
deleteFolderByFilePath(file);
|
} else {
|
// 删除文件
|
file.delete();
|
}
|
}
|
}
|
// 删除文件夹本身
|
folder.delete();
|
}
|
}
|
|
|
/**
|
* @param targetDir 目标目录
|
* @throws IOException
|
*/
|
private void createDirectoriesIfNotExists(String targetDir) throws IOException {
|
Path path = Paths.get(targetDir);
|
if (!Files.exists(path)) {
|
Files.createDirectories(path);
|
}
|
}
|
|
private Boolean directoriesIfNotExists(String targetDir) throws IOException {
|
Path path = Paths.get(targetDir);
|
if (!Files.exists(path)) {
|
// Files.createDirectories(path);
|
return false;
|
} else {
|
return true;
|
}
|
}
|
|
|
}
|