开发手账(一)

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

相关文章

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T