开发手账(一)

2023-11-21 20:04
文章标签 开发 手账

本文主要是介绍开发手账(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、 关于设计

(一)数据库

  1. 确定外键标识,需判断该外键是否有可能被修改。如菜单id,菜单code,菜单名,前两者都可做外键,后面一个则不应做外键。

二、关于组件

(一)POI

1. 文档页数统计

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.ofdrw.reader.OFDReader;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
@Slf4j
public class LvDocPageCounter {public static final String DOCUMENT_PAGE_TEMP = "DOCUMENT_PAGE_TEMP";public static int getPageCount(String filePath) {String fileType = getFileType(filePath);try {switch (fileType) {case "pdf":return getPdfPageCount(filePath);case "docx":return getDocxPageCount(filePath);case "doc":return getDocPageCount(filePath);case "ofd":return getOfdPageCount(filePath);// Add more cases for other document types as neededdefault:log.warn("不支持的文件类型:{}", filePath);return 1;
//                throw new IllegalArgumentException("Unsupported file type");}} catch (Exception e) {log.warn("读取文件异常:{},{}", filePath,e);return 0;}}/*** 文件类型* @param filePath* @return*/private static String getFileType(String filePath) {int dotIndex = filePath.lastIndexOf('.');if (dotIndex == -1 || dotIndex == filePath.length() - 1) {log.warn("文件名中没有找到扩展名:{}", filePath);return "";}return filePath.substring(dotIndex + 1).toLowerCase();}/*** 获取PDF文档页数* @param filePath* @return* @throws IOException*/private static int getPdfPageCount(String filePath) throws IOException {try (PDDocument document = Loader.loadPDF(new File(filePath))) {
//            PDDocument document = new PDDocument();int numberOfPages = document.getNumberOfPages();document.close();return numberOfPages;}}/*** 获取doc文档页数* @param filePath* @return* @throws IOException*/private static int getDocPageCount(String filePath) throws IOException {
//        try (InputStream inputStream = new FileInputStream(filePath);
//             HWPFDocument document = new HWPFDocument(inputStream)) {
//            int pageCount = document.getSummaryInformation().getPageCount();
//            document.close();
//            return pageCount;
//        }try (InputStream inputStream = new FileInputStream(filePath)) {com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);int num = doc.getPageCount();doc.cleanup();return num;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 获取docx页数* @param filePath* @return* @throws IOException*/private static int getDocxPageCount(String filePath) throws IOException {
//        try (InputStream inputStream = new FileInputStream(filePath);
//             XWPFDocument document = new XWPFDocument(inputStream)) {
//            int pages = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
//            document.close();
//            return pages;
//        }try (InputStream inputStream = new FileInputStream(filePath)) {com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);int num = doc.getPageCount();doc.cleanup();return num;} catch (Exception e) {e.printStackTrace();return 0;}}/*** pdf页数* @param filePath* @return* @throws IOException*/private static int getOfdPageCount(String filePath) throws IOException {Path ofdFile = Paths.get(filePath);OFDReader ofdReader = new OFDReader(ofdFile);int numberOfPages = ofdReader.getNumberOfPages();ofdReader.close();return numberOfPages;}/*** 获取缓存文件页数* @param inputStream* @param originalFilename* @return*/public static Integer getPageCount(MultipartFile inputStream, String originalFilename) {try (InputStream inputStream1 = inputStream.getInputStream()) {return getPageCount(inputStream1,originalFilename);} catch (IOException e) {log.warn("读取文件异常:{},{}", originalFilename,e);return 0;}}// Add methods for other document types as needed
}

2. 文本提取

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.ofdrw.converter.export.TextExporter;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;/*** @author yilv* @version 1.0* @description: TODO* @date 2023/11/16 16:12*/
@Slf4j
public class LvDocTxTHunter {private static AtomicInteger  UPPER_LIMIT=new AtomicInteger(50);/*** 读取文档内容* @param filePath* @return*/public static String readText(String filePath) {int pageCount = LvDocPageCounter.getPageCount(filePath);if (pageCount >UPPER_LIMIT.get()) {log.warn("文件过大:{},{}", filePath,pageCount);return "";}String fileType = getFileType(filePath);try {switch (fileType) {case "pdf":return readPdfText(filePath);case "doc":return readDocText(filePath);case "docx":return readDocxText(filePath);case "ofd":return readOfdText(filePath);// Add more cases for other document types as neededdefault:log.warn("不支持的文件类型:{}", filePath);return "";}} catch (IOException e) {log.warn("读取文件异常:{},{}", filePath,e);return "";}}/*** 获取文件类型* @param filePath* @return*/private static String getFileType(String filePath) {int dotIndex = filePath.lastIndexOf('.');if (dotIndex == -1 || dotIndex == filePath.length() - 1) {log.warn("文件名中没有找到扩展名:{}", filePath);return "";}return filePath.substring(dotIndex + 1).toLowerCase();}/*** 获取pdf文本* @param filePath* @return* @throws IOException*/private static String readPdfText(String filePath) throws IOException {try (PDDocument document = Loader.loadPDF(filePath)) {String text = new PDFTextStripper().getText(document);document.close();return text;}}/*** 获取doc文本* @param filePath* @return* @throws IOException*/private static String readDocText(String filePath) throws IOException {try (InputStream inputStream = new FileInputStream(filePath);HWPFDocument document = new HWPFDocument(inputStream)) {WordExtractor extractor = new WordExtractor(document);String text = extractor.getText();document.close();return text;}}/*** 获取docx文本* @param filePath* @return* @throws IOException*/private static String readDocxText(String filePath) throws IOException {try (InputStream inputStream = new FileInputStream(filePath);XWPFDocument document = new XWPFDocument(inputStream)) {XWPFWordExtractor extractor = new XWPFWordExtractor(document);String text = extractor.getText();document.close();return text;}}/*** pdf页数* @param filePath* @return* @throws IOException*/private static String readOfdText(String filePath) throws IOException {Path txtPath = Paths.get("DOCUMENT_PAGE_TEMP", FilenameUtils.getBaseName(filePath) + ".txt");TextExporter textExporter = new TextExporter(Paths.get(filePath), txtPath);textExporter.export();String s = FileUtil.readUtf8String(txtPath.toFile());textExporter.close();return s;}/*** 获取文件文本* @param tempFile* @return*/public static String readText(File tempFile) {return readText(tempFile.getPath());}// Add methods for other document types as needed
}

3. 文案转换

  • ofd转换
    • ①启动加载字体
    /*** 前置系统数据加载*/private static void systemInit() {FontLoader preload = FontLoader.Preload();preload.scanFontDir(Paths.get(FileUtil.local, "font"));Field namePathMapping = ReflectUtil.getField(FontLoader.class, "fontNamePathMapping");Map<String, String> fontNamePathMapping = (Map<String, String>) ReflectUtil.getFieldValue(preload,namePathMapping);System.out.println("加载字体:" + JSONUtil.toJsonStr(fontNamePathMapping.keySet()));}
    • ②使用ofdrw进行pdf转换
    /*** 将OFD转换为PDF** @param ofdPath OFD路径* @param distPath 输出路径* @param pdfPath 输出PDF路径* @throws IOException*/public static void convertOfdToPDFByBridge(String ofdPath, String distPath, String pdfPath) throws IOException {log.debug("解析文件:{}",ofdPath);Path ofdFilePath = Paths.get(ofdPath);Path dir = Paths.get(distPath);PDFExporterIText exporter = new PDFExporterIText(ofdFilePath, Paths.get(pdfPath));exporter.export();exporter.close();}

这篇关于开发手账(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/404759

相关文章

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录