GetElectronFile.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.kingdee.shr.custom.sendfile;
  2. import org.springframework.core.io.ByteArrayResource;
  3. import org.springframework.core.io.FileSystemResource;
  4. import org.springframework.core.io.Resource;
  5. import org.springframework.http.HttpHeaders;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.ResponseEntity;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.io.OutputStream;
  14. import java.net.URLEncoder;
  15. import java.nio.file.Files;
  16. import java.nio.file.Path;
  17. import java.nio.file.Paths;
  18. public class GetElectronFile {
  19. public static void handlePdfFile(String dir, String fileName, HttpServletResponse response, boolean isDownload) throws IOException {
  20. String home = System.getProperty("EAS_HOME"); // 获取 EAS 根目录
  21. String fileDir = home + "/server/deploy/easweb.ear/shr_web.war/tmp/electronicAttachment/" + dir;
  22. fileName = URLEncoder.encode(fileName, "UTF-8");
  23. File pdfFile = new File(fileDir, fileName);
  24. // 检查文件是否存在
  25. if (!pdfFile.exists() || !pdfFile.isFile()) {
  26. response.sendError(HttpServletResponse.SC_NOT_FOUND, "文件未找到或已失效");
  27. return;
  28. }
  29. // 设置响应类型和头部信息
  30. response.setContentType("application/pdf");
  31. String contentDisposition = isDownload ? "attachment" : "inline";
  32. response.setHeader("Content-Disposition", contentDisposition + "; filename=\"" + pdfFile.getName() + "\"");
  33. response.setContentLength((int) pdfFile.length());
  34. // 读取文件并写入响应流
  35. try (FileInputStream fileInputStream = new FileInputStream(pdfFile);
  36. OutputStream outputStream = response.getOutputStream()) {
  37. byte[] buffer = new byte[4096];
  38. int bytesRead;
  39. while ((bytesRead = fileInputStream.read(buffer)) != -1) {
  40. outputStream.write(buffer, 0, bytesRead);
  41. }
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "文件无法读取或发送");
  45. }
  46. }
  47. public ResponseEntity<Resource> viewPdf(String dir, String fileName) throws IOException {
  48. String home = System.getProperty("EAS_HOME"); // 获取 EAS 根目录
  49. String fileDir = home + "/server/deploy/easweb.ear/shr_web.war/tmp/electronicAttachment/" + dir;
  50. fileName = URLEncoder.encode(fileName, "UTF-8");
  51. Path filePath = Paths.get(fileDir, fileName);
  52. // 检查文件是否存在
  53. if (!Files.exists(filePath)) {
  54. return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  55. }
  56. // 检查文件是否可读
  57. if (!Files.isReadable(filePath)) {
  58. return new ResponseEntity<>(HttpStatus.FORBIDDEN);
  59. }
  60. // 读取文件内容到字节数组中
  61. byte[] fileContent = Files.readAllBytes(filePath);
  62. // 创建ByteArrayResource来包装字节数组
  63. Resource resource = new ByteArrayResource(fileContent);
  64. // 设置HTTP响应头
  65. HttpHeaders headers = new HttpHeaders();
  66. headers.add(HttpHeaders.CONTENT_TYPE, "application/pdf");
  67. // 创建并返回ResponseEntity
  68. return new ResponseEntity<>(resource, headers, HttpStatus.OK);
  69. }
  70. public ResponseEntity<Resource> downloadFile(String dir, String fileName) throws IOException {
  71. String home = System.getProperty("EAS_HOME"); // 获取 EAS 根目录
  72. String fileDir = home + "/server/deploy/easweb.ear/shr_web.war/tmp/electronicAttachment/" + dir;
  73. fileName = URLEncoder.encode(fileName, "UTF-8");
  74. Path filePath = Paths.get(fileDir, fileName);
  75. // 检查文件是否存在和可读
  76. if (!Files.exists(filePath) || !Files.isReadable(filePath)) {
  77. return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
  78. }
  79. // 获取文件的内容类型
  80. String contentType = Files.probeContentType(filePath);
  81. if (contentType == null) {
  82. contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
  83. }
  84. // 创建资源对象
  85. Resource resource = new FileSystemResource(filePath.toFile());
  86. // 设置HTTP响应头
  87. HttpHeaders headers = new HttpHeaders();
  88. headers.add(HttpHeaders.CONTENT_TYPE, contentType);
  89. headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
  90. // 创建并返回ResponseEntity
  91. return ResponseEntity.ok()
  92. .headers(headers)
  93. .body(resource);
  94. }
  95. }