|
@@ -1,27 +1,39 @@
|
|
|
package com.macro.mall.tiny.modules.business.controller;
|
|
|
|
|
|
import com.macro.mall.tiny.common.api.CommonResult;
|
|
|
+import com.macro.mall.tiny.config.UploadConfig;
|
|
|
import com.macro.mall.tiny.domain.AdminUserDetails;
|
|
|
+import com.macro.mall.tiny.modules.business.dto.FileDownloadParam;
|
|
|
import com.macro.mall.tiny.modules.business.dto.FileUploadParam;
|
|
|
import com.macro.mall.tiny.modules.business.model.BDirectory;
|
|
|
-import com.macro.mall.tiny.modules.business.service.BDirectoryService;
|
|
|
+import com.macro.mall.tiny.modules.business.model.BFile;
|
|
|
+import com.macro.mall.tiny.modules.business.model.BFileDetail;
|
|
|
import com.macro.mall.tiny.modules.business.service.FileService;
|
|
|
+import com.macro.mall.tiny.security.util.AuthUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.List;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
/**
|
|
|
* 大文件上传
|
|
@@ -35,8 +47,8 @@ public class FileController {
|
|
|
@Autowired
|
|
|
private FileService fileService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private BDirectoryService directoryService;
|
|
|
+ @Value("${system.charset:gbk}")
|
|
|
+ private String charset;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -97,7 +109,109 @@ public class FileController {
|
|
|
@GetMapping("/getAllDirectory")
|
|
|
public CommonResult<BDirectory> getDirectory() {
|
|
|
AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
- final BDirectory directory = directoryService.getDirectory(userDetails);
|
|
|
+ final BDirectory directory = fileService.getRootDirectory(userDetails);
|
|
|
return CommonResult.success(directory);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ *@describe 获取文件夹下全被文件详细接口
|
|
|
+ *@param path
|
|
|
+ *@return com.macro.mall.tiny.common.api.CommonResult<java.util.List<com.macro.mall.tiny.modules.business.model.BFileDetail>>
|
|
|
+ *@author gjs
|
|
|
+ *@since J7.23.0
|
|
|
+ *@date 2021/4/5
|
|
|
+ */
|
|
|
+ @ApiOperation("获取文件夹下全部文件详细接口")
|
|
|
+ @GetMapping("/getFileDetail")
|
|
|
+ public CommonResult<List<BFileDetail>> getFileDetail(@RequestParam @PathParam(value = "path") String path) {
|
|
|
+ AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ List<BFileDetail> fileDetailList = fileService.getFileDetailList(path, userDetails);
|
|
|
+ return CommonResult.success(fileDetailList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("获取文件夹下全部文件/文件夹")
|
|
|
+ @GetMapping("/getFile")
|
|
|
+ public CommonResult<List<BFile>> getFile(@RequestParam @PathParam(value = "path") String path) {
|
|
|
+ AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ List<BFile> fileDetailList = fileService.getFileList(path, userDetails);
|
|
|
+ return CommonResult.success(fileDetailList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("文件下载接口")
|
|
|
+ @GetMapping("/download")
|
|
|
+ public void downLoad(HttpServletResponse response, @RequestParam @PathParam(value = "path") String path) throws Exception {
|
|
|
+ AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ boolean access = AuthUtil.checkAccess(userDetails, AuthUtil.ACCESS);
|
|
|
+ if (path.startsWith(String.valueOf(UploadConfig.AUTH_STR))&&!access) return;
|
|
|
+ File file = new File(path);
|
|
|
+ if (file.exists()) { //判断文件父目录是否存在
|
|
|
+ String fileName = file.getName();
|
|
|
+ response.setContentType("application/form-data");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+ response.setHeader("Access-Control-Expose-Headers", "content-Disposition");
|
|
|
+ try(FileInputStream fileInputStream = new FileInputStream(file)){
|
|
|
+ try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
|
|
|
+ try (OutputStream outputStream = response.getOutputStream()){
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int read = bufferedInputStream.read(buffer);
|
|
|
+ while (read != -1) {
|
|
|
+ outputStream.write(buffer);
|
|
|
+ read = bufferedInputStream.read(buffer);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("多文件压缩下载接口")
|
|
|
+ @PostMapping("/multiFileZipDownload")
|
|
|
+ public void multiFileZipDownload(HttpServletResponse response, @Validated @RequestBody FileDownloadParam fileDownloadParam) throws Exception {
|
|
|
+ AdminUserDetails userDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ boolean access = AuthUtil.checkAccess(userDetails, AuthUtil.ACCESS);
|
|
|
+ OutputStream outputStream = response.getOutputStream();
|
|
|
+ ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream, Charset.forName(charset));
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileDownloadParam.getName(), "UTF-8"));
|
|
|
+ response.setHeader("Access-Control-Expose-Headers", "content-Disposition");
|
|
|
+ try {
|
|
|
+ for (String path : fileDownloadParam.getFileList()) {
|
|
|
+ Path file = Paths.get(path);
|
|
|
+ String fileName = file.getFileName().toString();
|
|
|
+ if (fileName.startsWith(String.valueOf(UploadConfig.AUTH_STR)) && !access) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (Files.isDirectory(file)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try (InputStream inputStream = Files.newInputStream(file)) {
|
|
|
+ // 创建一个压缩项,指定名称
|
|
|
+ int count = file.getNameCount();
|
|
|
+ StringBuilder filePathSB = new StringBuilder();
|
|
|
+ for (int i = 2; i < count - 1; i++) {
|
|
|
+ filePathSB.append(file.getName(i)).append("/");
|
|
|
+ }
|
|
|
+ String filePath = filePathSB.toString() + fileName;
|
|
|
+ ZipEntry zipEntry = new ZipEntry(filePath);
|
|
|
+ // 添加到压缩流
|
|
|
+ zipOutputStream.putNextEntry(zipEntry);
|
|
|
+ // 写入数据
|
|
|
+ int len;
|
|
|
+ byte[] buffer = new byte[1024 * 10];
|
|
|
+ while ((len = inputStream.read(buffer)) > 0) {
|
|
|
+ zipOutputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ zipOutputStream.flush();
|
|
|
+ }
|
|
|
+ // 完成所有压缩项的添加
|
|
|
+ zipOutputStream.closeEntry();
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ zipOutputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|