| | |
| | | package com.example.erp.controller.pp; |
| | | |
| | | import cn.dev33.satoken.annotation.SaCheckPermission; |
| | | import org.springframework.core.io.InputStreamResource; |
| | | import com.example.erp.common.Constants; |
| | | import com.example.erp.common.Result; |
| | | import com.example.erp.entity.pp.LayoutsData; |
| | |
| | | import com.example.erp.service.pp.JsonToOptConverter; |
| | | import com.example.erp.service.userInfo.UserService; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.sql.Date; |
| | | import java.util.Map; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | @CrossOrigin(origins = "*") |
| | | @ApiOperation("PDF文件下载接口") |
| | | @GetMapping("/reports/pdf") |
| | | public ResponseEntity<InputStreamResource> downloadPdf(@RequestParam String filePath) { |
| | | try { |
| | | String decodedPath = java.net.URLDecoder.decode(filePath, "UTF-8"); |
| | | System.out.println("接收到的文件路径: " + decodedPath); |
| | | |
| | | // 放宽验证条件,仅检查必要条件 |
| | | if (decodedPath == null || decodedPath.trim().isEmpty()) { |
| | | System.out.println("文件路径为空"); |
| | | return ResponseEntity.badRequest().build(); |
| | | } |
| | | |
| | | // 检查是否为PDF文件(不严格检查开头) |
| | | if (!decodedPath.trim().toLowerCase().endsWith(".pdf")) { |
| | | System.out.println("文件不是PDF格式: " + decodedPath); |
| | | return ResponseEntity.badRequest().build(); |
| | | } |
| | | |
| | | // 处理路径,正确处理Windows网络路径 |
| | | String processedPath = decodedPath.trim(); |
| | | // 确保网络路径格式正确 |
| | | if (processedPath.startsWith("\\\\")) { |
| | | // 已经是正确的网络路径格式 |
| | | System.out.println("检测到Windows网络路径: " + processedPath); |
| | | } else if (processedPath.startsWith("\\")) { |
| | | // 补全网络路径 |
| | | processedPath = "\\" + processedPath; |
| | | System.out.println("补全网络路径为: " + processedPath); |
| | | } |
| | | |
| | | System.out.println("处理后的文件路径: " + processedPath); |
| | | |
| | | // 构造文件路径 |
| | | File file = new File(processedPath); |
| | | System.out.println("构造的文件绝对路径: " + file.getAbsolutePath()); |
| | | System.out.println("文件规范路径: " + file.getCanonicalPath()); |
| | | |
| | | // 检查文件是否存在 |
| | | if (!file.exists()) { |
| | | System.out.println("文件不存在: " + file.getAbsolutePath()); |
| | | return ResponseEntity.notFound().build(); |
| | | } |
| | | |
| | | // 检查是否为文件(而非目录) |
| | | if (!file.isFile()) { |
| | | System.out.println("路径不是文件: " + file.getAbsolutePath()); |
| | | return ResponseEntity.badRequest().build(); |
| | | } |
| | | |
| | | // 创建Resource |
| | | InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); |
| | | |
| | | // 设置响应头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + file.getName() + "\""); |
| | | headers.add(HttpHeaders.CONTENT_TYPE, "application/pdf"); |
| | | headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length())); |
| | | headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); |
| | | |
| | | return ResponseEntity.ok() |
| | | .headers(headers) |
| | | .contentType(org.springframework.http.MediaType.APPLICATION_PDF) |
| | | .body(resource); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |