|
@@ -0,0 +1,95 @@
|
|
1
|
+package com.ruoyi.web.controller.utils;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import com.ruoyi.common.utils.uuid.UUID;
|
|
5
|
+import lombok.extern.log4j.Log4j2;
|
|
6
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
7
|
+import org.springframework.web.multipart.MultipartFile;
|
|
8
|
+
|
|
9
|
+import java.io.*;
|
|
10
|
+
|
|
11
|
+/**
|
|
12
|
+ * @Author: Ljx
|
|
13
|
+ * @Date: 2023/12/20
|
|
14
|
+ **/
|
|
15
|
+@Log4j2
|
|
16
|
+public class PdfUtils {
|
|
17
|
+
|
|
18
|
+ /**
|
|
19
|
+ * 获取不带扩展名的文件名
|
|
20
|
+ */
|
|
21
|
+ public static String getFileNameNoSuffix(String filename) {
|
|
22
|
+ if ((filename != null) && (filename.length() > 0)) {
|
|
23
|
+ int dot = filename.lastIndexOf('.');
|
|
24
|
+ if ((dot > -1) && (dot < (filename.length()))) {
|
|
25
|
+ return filename.substring(0, dot);
|
|
26
|
+ }
|
|
27
|
+ }
|
|
28
|
+ return filename;
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ /**
|
|
32
|
+ * 获取文件扩展名
|
|
33
|
+ */
|
|
34
|
+ public static String getSuffixNameName(String filename) {
|
|
35
|
+ if ((filename != null) && (filename.length() > 0)) {
|
|
36
|
+ int dot = filename.lastIndexOf('.');
|
|
37
|
+ if ((dot > -1) && (dot < (filename.length() - 1))) {
|
|
38
|
+ return filename.substring(dot + 1);
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ return filename;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * File转MultipartFile
|
|
47
|
+ *
|
|
48
|
+ * @param mulFile 文件对象
|
|
49
|
+ * @return Multipart文件对象
|
|
50
|
+ */
|
|
51
|
+ public static File multipartFileToFile(MultipartFile mulFile) throws IOException {
|
|
52
|
+ InputStream ins = mulFile.getInputStream();
|
|
53
|
+ String fileName = mulFile.getOriginalFilename();
|
|
54
|
+ String prefix = getFileNameNoSuffix(fileName) + UUID.randomUUID().toString();
|
|
55
|
+ String suffix = "." + getSuffixNameName(fileName);
|
|
56
|
+ File toFile = File.createTempFile(prefix, suffix);
|
|
57
|
+ OutputStream os = new FileOutputStream(toFile);
|
|
58
|
+ int bytesRead = 0;
|
|
59
|
+ byte[] buffer = new byte[8192];
|
|
60
|
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
61
|
+ os.write(buffer, 0, bytesRead);
|
|
62
|
+ }
|
|
63
|
+ os.close();
|
|
64
|
+ ins.close();
|
|
65
|
+ return toFile;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+ /**
|
|
70
|
+ * 校验pdf文件是否包含js脚本
|
|
71
|
+ **/
|
|
72
|
+ public static boolean containsJavaScript(File file) throws IOException {
|
|
73
|
+
|
|
74
|
+// RandomAccessFile is = new RandomAccessFile(file, "r");
|
|
75
|
+ try (PDDocument document = PDDocument.load(file)){
|
|
76
|
+ String CosName = document.getDocument().getTrailer().toString();
|
|
77
|
+ if (CosName.contains("COSName{JavaScript}")
|
|
78
|
+ || CosName.contains("COSName{JS}")
|
|
79
|
+ || CosName.contains("COSName{alert}")
|
|
80
|
+ || CosName.contains("COSName{onmouseover}")
|
|
81
|
+ || CosName.contains("COSName{alalertert}")
|
|
82
|
+ || CosName.contains("COSName{embed}")
|
|
83
|
+ ) {
|
|
84
|
+ return true;
|
|
85
|
+ }
|
|
86
|
+ } catch (Exception e) {
|
|
87
|
+ log.error("PDF效验异常:" + e.getMessage());
|
|
88
|
+ return true;
|
|
89
|
+ }
|
|
90
|
+ return false;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+}
|