|
@@ -14,12 +14,21 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
|
|
+import org.springframework.core.io.UrlResource;
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.util.Arrays;
|
|
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
@@ -188,5 +197,36 @@ public class AitagTagInfoController {
|
|
|
return ResultDto.success(aitagTagInfoService.generateRegex(regexVo));
|
|
return ResultDto.success(aitagTagInfoService.generateRegex(regexVo));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量导入模板下载
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return ResultDto
|
|
|
|
|
+ */
|
|
|
|
|
+ @ApiOperation("批量导入模板下载")
|
|
|
|
|
+ @GetMapping("/downloadFile")
|
|
|
|
|
+ public ResponseEntity<Resource> downloadFile( ) throws IOException {
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 构造文件路径并进行规范化,防止路径遍历攻击 (如 ../../恶意路径)
|
|
|
|
|
+ Path filePath = Paths.get("template").resolve("tag_batch_Import_template.xlsx").normalize();
|
|
|
|
|
+ ClassPathResource resource = new ClassPathResource(filePath.toString());
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 校验文件是否存在且可读
|
|
|
|
|
+ if (resource.exists() && resource.isReadable()) {
|
|
|
|
|
+ // 3. 推断文件的 MediaType (MIME type)
|
|
|
|
|
+ String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
|
|
|
|
|
+ contentType = Files.probeContentType(filePath);
|
|
|
|
|
+ // 4. 返回 ResponseEntity,设置响应头
|
|
|
|
|
+ return ResponseEntity.ok()
|
|
|
|
|
+ .contentType(MediaType.parseMediaType(contentType))
|
|
|
|
|
+ // 告知浏览器以附件形式下载,并指定文件名
|
|
|
|
|
+ // 使用 URLEncoder.encode 处理中文文件名乱码问题
|
|
|
|
|
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""
|
|
|
|
|
+ + URLEncoder.encode("tag_batch_Import_template.xlsx", StandardCharsets.UTF_8.toString()) + "\"")
|
|
|
|
|
+ .body(resource);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 文件不存在或不可读,返回 404
|
|
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|