日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

用Java驗證pdf文件的電子章簽名

瀏覽:144日期:2022-08-19 09:07:24
pom.xml

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.yalong</groupId> <artifactId>verifyPdf</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <lombok.version>1.18.10</lombok.version> </properties> <dependencies> <!-- <dependency>--> <!-- <groupId> e-iceblue </groupId>--> <!-- <artifactId>spire.pdf</artifactId>--> <!-- <version>3.4.2</version>--> <!-- </dependency>--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>2.6.3</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> <repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories></project>VerifySignature.java

import com.spire.pdf.PdfDocument;import com.spire.pdf.security.PdfCertificate;import com.spire.pdf.security.PdfSignature;import com.spire.pdf.widget.PdfFormFieldWidgetCollection;import com.spire.pdf.widget.PdfFormWidget;import com.spire.pdf.widget.PdfSignatureFieldWidget;import lombok.Data;import lombok.ToString;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.xssf.streaming.SXSSFWorkbook;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.Serializable;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashSet;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.*;import java.awt.*;@Data@ToStringclass ExcelDataVO implements Serializable { private String fileName; private String signDate; private String validBefore; private String validAfter; private String subject; private String serialNumber; private Boolean isEffective = false;}class ExcelWriter { //表頭 private static final List<String> CELL_HEADS; static { // 類裝載時就載入指定好的表頭信息,如有需要,可以考慮做成動態(tài)生成的表頭 CELL_HEADS = new ArrayList<>(); CELL_HEADS.add('文件名'); CELL_HEADS.add('簽名時間'); CELL_HEADS.add('有效期'); CELL_HEADS.add('有效期'); CELL_HEADS.add('簽名機構(gòu)'); CELL_HEADS.add('序列號'); CELL_HEADS.add('是否通過驗簽'); } /** * 生成Excel并寫入數(shù)據(jù)信息 * * @param dataList 數(shù)據(jù)列表 * @return 寫入數(shù)據(jù)后的工作簿對象 */ public static Workbook exportData(List<ExcelDataVO> dataList) { // 生成xlsx的Excel Workbook workbook = new SXSSFWorkbook(); // 如需生成xls的Excel,請使用下面的工作簿對象,注意后續(xù)輸出時文件后綴名也需更改為xls //Workbook workbook = new HSSFWorkbook(); // 生成Sheet表,寫入第一行的表頭 Sheet sheet = buildDataSheet(workbook); //構(gòu)建每行的數(shù)據(jù)內(nèi)容 int rowNum = 1; for (ExcelDataVO data : dataList) { if (data == null) {continue; } //輸出行數(shù)據(jù) Row row = sheet.createRow(rowNum++); convertDataToRow(workbook, data, row); } return workbook; } /** * 生成sheet表,并寫入第一行數(shù)據(jù)(表頭) * * @param workbook 工作簿對象 * @return 已經(jīng)寫入表頭的Sheet */ private static Sheet buildDataSheet(Workbook workbook) { Sheet sheet = workbook.createSheet(); // 設(shè)置表頭寬度 for (int i = 0; i < CELL_HEADS.size(); i++) { sheet.setColumnWidth(i, 4000); } // 設(shè)置默認行高 sheet.setDefaultRowHeight((short) 400); // 構(gòu)建頭單元格樣式 CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook()); // 寫入第一行各列的數(shù)據(jù) Row head = sheet.createRow(0); for (int i = 0; i < CELL_HEADS.size(); i++) { Cell cell = head.createCell(i); cell.setCellValue(CELL_HEADS.get(i)); cell.setCellStyle(cellStyle); } return sheet; } /** * 設(shè)置第一行表頭的樣式 * * @param workbook 工作簿對象 * @return 單元格樣式對象 */ private static CellStyle buildHeadCellStyle(Workbook workbook) { CellStyle style = workbook.createCellStyle(); //對齊方式設(shè)置 style.setAlignment(HorizontalAlignment.CENTER); //邊框顏色和寬度設(shè)置 style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下邊框 style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左邊框 style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右邊框 style.setBorderTop(BorderStyle.THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上邊框 //設(shè)置背景顏色 style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //粗體字設(shè)置 Font font = workbook.createFont(); font.setBold(true); style.setFont(font); return style; } /** * 將數(shù)據(jù)轉(zhuǎn)換成行 * * @param data 源數(shù)據(jù) * @param row 行對象 */ private static void convertDataToRow(Workbook workbook, ExcelDataVO data, Row row) { int cellNum = 0; Cell cell; //對特殊數(shù)值設(shè)置顏色 CellStyle cellStyle = workbook.createCellStyle(); //字體設(shè)置 Font font = workbook.createFont(); font.setBold(true); font.setColor(IndexedColors.GREEN.getIndex()); cellStyle.setFont(font); // 文件名 cell = row.createCell(cellNum++); cell.setCellValue(data.getFileName()); // 簽名時間 cell = row.createCell(cellNum++); cell.setCellValue(null == data.getSignDate() ? '' : data.getSignDate()); // 有效期 cell = row.createCell(cellNum++); cell.setCellValue(null == data.getValidBefore() ? '' : data.getValidBefore()); // 有效期 cell = row.createCell(cellNum++); cell.setCellValue(null == data.getValidAfter() ? '' : data.getValidAfter()); //主題 cell = row.createCell(cellNum++); cell.setCellValue(null == data.getSubject() ? '' : data.getSubject()); //序列號 cell = row.createCell(cellNum++); cell.setCellValue(null == data.getSerialNumber() ? '' : data.getSerialNumber()); //是否通過驗簽 cell = row.createCell(cellNum); if (data.getIsEffective()) { cell.setCellValue('簽名有效'); } else { cell.setCellValue('簽名無效'); cell.setCellStyle(cellStyle); } } public static void writeExcel(List<ExcelDataVO> dataVOList, String exportFilePath) { // 寫入數(shù)據(jù)到工作簿對象內(nèi) Workbook workbook = ExcelWriter.exportData(dataVOList); // 以文件的形式輸出工作簿對象 FileOutputStream fileOut = null; try { File exportFile = new File(exportFilePath); if (!exportFile.exists()) {boolean newFile = exportFile.createNewFile();if (!newFile) { System.out.println('文件創(chuàng)建失敗');} } fileOut = new FileOutputStream(exportFilePath); workbook.write(fileOut); fileOut.flush(); } catch (Exception e) { System.out.println('輸出Excel時發(fā)生錯誤,錯誤原因:' + e.getMessage()); } finally { try {if (null != fileOut) { fileOut.close();}workbook.close(); } catch (IOException e) {System.out.println('關(guān)閉輸出流時發(fā)生錯誤,錯誤原因:' + e.getMessage()); } } }}public class VerifySignature { private static String fromDirPath; private static String toFilePath; public static void main(String[] args) { final JFrame jf = new JFrame('測試窗口'); jf.setSize(400, 250); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(); // 創(chuàng)建文本區(qū)域, 用于顯示相關(guān)信息 final JTextArea msgTextArea = new JTextArea(10, 30); msgTextArea.setLineWrap(true); panel.add(msgTextArea); JButton openBtn = new JButton('選擇文件路徑'); openBtn.addActionListener(e -> showFileOpenDialog(jf, msgTextArea)); panel.add(openBtn); JButton saveBtn = new JButton('結(jié)果保存位置'); saveBtn.addActionListener(e -> showFileSaveDialog(jf, msgTextArea)); panel.add(saveBtn); jf.setContentPane(panel); jf.setVisible(true); JButton enSureBtn = new JButton('確認'); enSureBtn.addActionListener(e -> enSureListener(jf)); panel.add(enSureBtn); jf.setContentPane(panel); jf.setVisible(true); } /* * 打開文件 */ private static void showFileOpenDialog(Component parent, JTextArea msgTextArea) { // 創(chuàng)建一個默認的文件選取器 JFileChooser fileChooser = new JFileChooser(); // 設(shè)置默認顯示的文件夾為當(dāng)前文件夾 fileChooser.setCurrentDirectory(new File('.')); // 設(shè)置文件選擇的模式(只選文件、只選文件夾、文件和文件均可選) fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 設(shè)置是否允許多選 fileChooser.setMultiSelectionEnabled(false);// // 添加可用的文件過濾器(FileNameExtensionFilter 的第一個參數(shù)是描述, 后面是需要過濾的文件擴展名 可變參數(shù))// fileChooser.addChoosableFileFilter(new FileNameExtensionFilter('zip(*.zip, *.rar)', 'zip', 'rar'));//// // 設(shè)置默認使用的文件過濾器// fileChooser.setFileFilter(new FileNameExtensionFilter('image(*.jpg, *.png, *.gif)', 'jpg', 'png', 'gif')); // 打開文件選擇框(線程將被阻塞, 直到選擇框被關(guān)閉) int result = fileChooser.showOpenDialog(parent); if (result == JFileChooser.APPROVE_OPTION) { // 如果點擊了'確定', 則獲取選擇的文件路徑 File file = fileChooser.getSelectedFile(); fromDirPath = file.getAbsolutePath(); msgTextArea.append('選擇源文件: ' + fromDirPath + 'nn'); } } /* * 選擇文件保存路徑 */ private static void showFileSaveDialog(Component parent, JTextArea msgTextArea) { // 創(chuàng)建一個默認的文件選取器 JFileChooser fileChooser = new JFileChooser(); //把時間戳經(jīng)過處理得到期望格式的時間 Date date = new Date(); SimpleDateFormat format0 = new SimpleDateFormat('yyyyMMddHHmmss'); String now = format0.format(date.getTime()); // 設(shè)置打開文件選擇框后默認輸入的文件名 fileChooser.setSelectedFile(new File(now + '.xlsx')); // 打開文件選擇框(線程將被阻塞, 直到選擇框被關(guān)閉) int result = fileChooser.showSaveDialog(parent); if (result == JFileChooser.APPROVE_OPTION) { // 如果點擊了'保存', 則獲取選擇的保存路徑 File file = fileChooser.getSelectedFile(); toFilePath = file.getAbsolutePath(); msgTextArea.append('結(jié)果文件路徑: ' + toFilePath + 'nn'); } } //找到需要的內(nèi)容 public final static Pattern pattern = Pattern.compile('[Subject].*?O=(.*?),.*?[Issuer](.*?)[Serial Number](.*?)[Not Before](.*?)[Not After](.*?)[Thumbprint](.*?)'); // 剔除特殊字符 public final static Pattern replacePattern = Pattern.compile('t|r|n'); /** * 查找某個路徑下的所有pdf文件 * * @return 所有的pdf絕對路徑 */ public static HashSet<String> listDir(String path) { HashSet<String> FileNameString = new HashSet<String>(); File file = new File(path); //獲取其file對象 File[] fs = file.listFiles(); //遍歷path下的文件和目錄,放在File數(shù)組中 if (fs == null) { System.out.println(path + '路徑下沒有文件'); return null; } //遍歷File[]數(shù)組 for (File f : fs) { String fileName = String.valueOf(f); if (!f.isDirectory() && fileName.toLowerCase().endsWith('.pdf')) //若非目錄(即文件),則打印FileNameString.add(fileName); } return FileNameString; } /** * 檢驗pdf文件是否簽名 * * @param filePath pdf文件絕對路徑 */ public static ExcelDataVO checkPdf(String filePath) { //創(chuàng)建PdfDocument實例 PdfDocument doc = new PdfDocument(); //創(chuàng)建結(jié)果集 ExcelDataVO excelDataVO = new ExcelDataVO(); //文件名,注意windows下應(yīng)該是,linux下是/ String fileName = filePath.substring(filePath.lastIndexOf('') + 1); excelDataVO.setFileName(fileName); //加載含有簽名的PDF文件 doc.loadFromFile(filePath); //獲取域集合 PdfFormWidget pdfFormWidget = (PdfFormWidget) doc.getForm(); PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.getFieldsWidget();// int countCollection = pdfFormFieldWidgetCollection.getCount();// System.out.println('共發(fā)現(xiàn)' + countCollection + '個域'); //遍歷域 for (int i = 0; i < pdfFormFieldWidgetCollection.getCount(); i++) { //判定是否為簽名域 if (pdfFormFieldWidgetCollection.get(i) instanceof PdfSignatureFieldWidget) {//獲取簽名域PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget) pdfFormFieldWidgetCollection.get(i);//獲取簽名時間PdfSignature signature = signatureFieldWidget.getSignature();excelDataVO.setSignDate(String.valueOf(signature.getDate()));//獲取簽名的內(nèi)容PdfCertificate certificate = signature.getCertificate();// System.out.println('Issuer:' + certificate.getIssuer());//System.out.println('Subject:' + certificate.getSubject());//System.out.println('---------');//excelDataVO.setSubject(String.valueOf(certificate.getSubject()));String certificateString = certificate.toString();Matcher m = replacePattern.matcher(certificateString);certificateString = m.replaceAll('');Matcher matcher = pattern.matcher(certificateString);while (matcher.find()) {// String group = matcher.group(0); String subject = matcher.group(1);// String issuer = matcher.group(2); String serialNumber = matcher.group(3); String before = matcher.group(4); String after = matcher.group(5);// String sha1 = matcher.group(6); excelDataVO.setSubject(subject); excelDataVO.setSerialNumber(serialNumber); excelDataVO.setValidBefore(before); excelDataVO.setValidAfter(after);}//判定簽名是否有效boolean result = signature.verifySignature();excelDataVO.setIsEffective(result);if (result) { return excelDataVO;} } } return excelDataVO; } /* * 開始執(zhí)行業(yè)務(wù)邏輯 */ private static void enSureListener(JFrame parent) { parent.dispose(); System.out.println('開始驗簽...'); //從某個路徑下獲取所有的pdf文件路徑 HashSet<String> filePaths = listDir(fromDirPath); if (filePaths == null) { return; } List<ExcelDataVO> excelDataVOS = new ArrayList<>(); for (String filePath : filePaths) { ExcelDataVO excelDataVO = checkPdf(filePath); excelDataVOS.add(excelDataVO); } ExcelWriter.writeExcel(excelDataVOS, toFilePath); System.out.println('驗簽完成...'); }}

以上就是用Java驗證pdf文件的電子章簽名的詳細內(nèi)容,更多關(guān)于Java驗證pdf文件的電子章簽名的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
91免费精品国偷自产在线在线| 欧美精品自拍| 蜜臀av性久久久久蜜臀aⅴ流畅 | 成人午夜毛片| 久久不卡日韩美女| 国产精品午夜一区二区三区| 爽爽淫人综合网网站| 免费在线观看日韩欧美| 中文字幕成人| 7777精品| 久久99国产精品视频| 精品欠久久久中文字幕加勒比| 麻豆国产欧美一区二区三区| 精品国产午夜| 麻豆国产在线| 欧美日韩国产在线观看网站| 欧美精品一线| 中文字幕免费一区二区| 69堂精品视频在线播放| 久久不见久久见国语| 精品久久91| 色乱码一区二区三区网站| av最新在线| 欧美精选一区二区三区| 手机精品视频在线观看| 日韩中文字幕一区二区高清99| 日韩av一区二区在线影视| 国产探花在线精品| 国产成人黄色| 欧美不卡高清| 亚洲综合婷婷| 麻豆视频一区二区| 青青青免费在线视频| 亚洲精品小说| 一区二区三区国产盗摄| 国产精品成人**免费视频| 精品九九在线| 极品日韩av| 日韩中文字幕| 久久尤物视频| 亚洲国产专区校园欧美| 日韩一区二区三区在线看| 国内精品亚洲| 99在线|亚洲一区二区| 日本a级不卡| 激情黄产视频在线免费观看| 米奇777超碰欧美日韩亚洲| 亚洲精品护士| 精品亚洲精品| 一区视频在线| 国产精品中文字幕制服诱惑| 成人精品高清在线视频| 九一国产精品| 日韩国产精品久久久久久亚洲| 欧美韩日一区| 日韩专区欧美专区| 福利精品在线| 亚洲深深色噜噜狠狠爱网站| 国产一区二区三区探花| 视频一区中文字幕| 女生影院久久| 国产女人18毛片水真多18精品| 韩国精品主播一区二区在线观看| 日韩极品在线观看| 亚洲成人一区| 精品一二三区| 免费精品视频| 国产精品蜜芽在线观看| 日韩精品免费视频人成| 九一国产精品| 激情中国色综合| 亚洲精品在线a| 久久精品一区二区不卡| 国产剧情一区| 视频精品一区二区| 亚洲播播91| 国产精品超碰| 日本成人精品| 亚洲在线一区| 久久人人88| 精品国产黄a∨片高清在线| 亚洲一二三区视频| 国产成人精品999在线观看| 日韩精品一区二区三区免费视频 | 六月婷婷综合| 日韩二区三区在线观看| 亚洲欧美日韩精品一区二区 | 久久久久亚洲| 精品中文字幕一区二区三区| 四虎在线精品| 99成人超碰| 精品一区二区三区在线观看视频| 日韩精品免费视频人成 | 亚洲神马久久| 欧美日韩免费观看视频| 免费视频一区二区三区在线观看| 中文字幕av一区二区三区人| 亚洲欧美一区在线| 日韩伦理一区| 国内一区二区三区| 国产精品中文| 日韩国产欧美一区二区三区| 视频一区二区三区入口| 97视频热人人精品免费| 国产精品magnet| 日韩高清不卡一区| 老司机久久99久久精品播放免费| 视频小说一区二区| 精品久久国产一区| 国产激情久久| 国产精品手机在线播放| 日韩av一区二区三区| 视频在线在亚洲| 好看的av在线不卡观看| 亚洲福利国产| 99精品视频在线观看免费播放| 91中文字幕精品永久在线| 精品黄色一级片| 欧美天堂在线| 国产探花在线精品一区二区| 日韩三级一区| 日韩中文字幕| 日韩激情一区二区| 亚洲ww精品| 亚洲精品亚洲人成在线观看| 丝袜美腿亚洲色图| 蜜桃视频一区二区三区在线观看| 一区在线免费| 美女精品网站| 四虎精品一区二区免费| 日韩精选在线| 国产探花一区在线观看| 国产精品一区二区99| 欧美日韩亚洲一区三区| 国产精品17p| 久久久久久色 | 亚洲免费一区三区| 日韩专区一卡二卡| 亚洲精品三级| 日韩1区2区3区| 91免费精品国偷自产在线在线| 97久久亚洲| 你懂的国产精品永久在线| 精品中文在线| 日韩免费小视频| 亚洲精品午夜av福利久久蜜桃| 亚洲综合二区| 日本在线成人| 久久久精品国产**网站| 亚洲精品成人图区| japanese国产精品| 亚洲字幕久久| 国产精品xxxav免费视频| 91视频一区| 999久久久亚洲| 久久亚洲不卡| 国产美女亚洲精品7777| 首页国产精品| 欧美日韩免费观看一区=区三区 | 亚洲婷婷丁香| 国产精品男女| 日本精品不卡| 老牛国产精品一区的观看方式| 国产日韩一区二区三区在线 | 蜜臀va亚洲va欧美va天堂| 青青草视频一区| 91日韩欧美| 老牛影视一区二区三区| 奇米色欧美一区二区三区| 国产成人久久精品麻豆二区 | 黄色欧美日韩| 国产区精品区| 99久久精品国产亚洲精品| 在线亚洲欧美| 国产亚洲一卡2卡3卡4卡新区| 国产精品久久久亚洲一区| 蜜桃av在线播放| 少妇精品久久久一区二区| 成人精品高清在线视频| 久久性天堂网| 精品国产精品久久一区免费式 | 欧美国产免费| 欧美aa国产视频| 日韩1区2区日韩1区2区| 国产一区二区三区91| 亚洲深夜影院| 精品一区二区三区的国产在线观看| 亚洲精品在线影院| 少妇精品在线| 亚洲精品国产嫩草在线观看 | 欧美国产小视频| 免费人成黄页网站在线一区二区| 国产精品一区二区精品| 亚洲电影在线一区二区三区| 国产麻豆一区二区三区| 久久精品动漫| 国产欧美日韩一级| 激情丁香综合| 麻豆精品久久久| 综合欧美精品|