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