java关于如何实现读取各种类型的文件核心属性方法,比如获取标题和作者、主题等;附带远程的https的地址文件读取方法;

news/2024/10/21 2:58:23/

有两种方法:
通过提供的现成api进行调用读取pdf文件,或doc、xlsx、pptx文件;可能商业需要付费
https://www.e-iceblue.cn/pdf_java_document_operation/set-pdf-document-properties-in-java.html
Spire.PDF for Java
在这里插入图片描述

java">import com.spire.pdf.*;
import java.io.*;public class getPDFProperties {public static void main(String[] args) throws IOException {//创建 PdfDocument 类的对象PdfDocument pdf = new PdfDocument();//从磁盘加载PDF文档pdf.loadFromFile("" + "setPDFProperties.pdf");//创建 StringBuilder 的对象以储存获取的属性数据StringBuilder stringBuilder = new StringBuilder();//获取PDF文档的属性数据并储存于创建的 StringBuilderstringBuilder.append("标题:" + pdf.getDocumentInformation().getTitle() + "\r\n");stringBuilder.append("作者" + pdf.getDocumentInformation().getAuthor() + "\r\n");stringBuilder.append("主题:" + pdf.getDocumentInformation().getSubject() + "\r\n");stringBuilder.append("关键词:" + pdf.getDocumentInformation().getKeywords() + "\r\n");stringBuilder.append("创建者:" + pdf.getDocumentInformation().getCreator() + "\r\n");stringBuilder.append("创建时间:" + pdf.getDocumentInformation().getCreationDate() + "\r\n");stringBuilder.append("制作工具:" + pdf.getDocumentInformation().getProducer() + "\r\n");//创建一个TXT文件File file = new File("getPDFProperties.txt");file.createNewFile();//将 StringBuilder 写入TXT文件FileWriter fileWriter = new FileWriter(file, true);BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);bufferedWriter.write(stringBuilder.toString());bufferedWriter.flush();}
}

第二种方法:
通过Apache POI进行读取实现;另外不同版本 方法实现也会有所不同;
Apache POI 的不同版本

引入依赖

java"><dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version> <!-- 请检查最新版本 --></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version> <!-- 检查是否有更新的版本 --></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.17.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version> <!-- 请检查最新版本 --><exclusions><exclusion><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId></exclusion></exclusions></dependency>

最后直接提供方法实现:

java">package com.ruoyi.project.backstage.pdf;import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.values.XmlComplexContentImpl;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;/*** @className: FileUtils* @author: 3.0* @date: 2024/10/16* @Version: 1.0* @description:*/public class FileUtils {//    public static void main(String[] args) {
//        try (PDDocument document = PDDocument.load(new File("E:\\project\\" + "1.pdf"))) {
//            PDDocumentInformation info = document.getDocumentInformation();
//            System.out.println("Title: " + info.getTitle());
//            System.out.println("Author: " + info.getAuthor());
//            System.out.println("Subject: " + info.getSubject());
//            // 其他属性...
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }//    public static void main(String[] args) throws Exception {
//        XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("E:\\project\\log.sh用法(1).docx")));
        CTProperties coreProps = doc.getProperties().getCoreProperties();
//        POIXMLProperties.CoreProperties coreProps = doc.getProperties().getCoreProperties();
//        System.out.println("Title: " + coreProps.getTitle());
//        System.out.println("Author: " + coreProps.getCreator());
//        System.out.println("主题: " + coreProps.getSubject());
//        // 更多属性...
//        doc.close();
//    }public static void main(String[] args) throws Exception {Workbook workbook = WorkbookFactory.create(new FileInputStream(new File("E:\\project\\工作簿1 - 副本.xls")));if (workbook instanceof POIXMLDocument) {POIXMLDocument poixmlDocument = (POIXMLDocument) workbook;POIXMLProperties properties = poixmlDocument.getProperties();POIXMLProperties.CoreProperties coreProperties = properties.getCoreProperties();// 现在你可以访问核心属性了String title = coreProperties.getTitle();String subject = coreProperties.getSubject();String creator = coreProperties.getCreator();// ... 其他属性// 打印属性到控制台System.out.println("Title: " + title);System.out.println("Subject: " + subject);System.out.println("Creator: " + creator);// ...} else {System.out.println("The workbook is not a POIXMLDocument (not an .xlsx file?).");}// 关闭工作簿(在try-with-resources中自动关闭fis,但这里显式关闭workbook以强调)workbook.close();}
//
//    public static void main(String[] args) throws Exception {
//        XMLSlideShow ppt = new XMLSlideShow(OPCPackage.open(new FileInputStream(new File("E:\\project\\演示文稿1.pptx"))));
//        System.out.println("Title: " + ppt.getProperties().getCoreProperties().getTitle());
//        System.out.println("Author: " + ppt.getProperties().getCoreProperties().getCreator());
//        System.out.println("主题: " + ppt.getProperties().getCoreProperties().getSubject());
//        // 更多属性...
//        ppt.close();
//    }}

如果有需要读取https地址的需求;可以实现下面的:
从远程 HTTPS URL 读取文件并将其转换为 FileInputStream 对象,你可以先将远程文件下载到本地磁盘,然后再使用 FileInputStream 打开它。以下是实现这一过程的一种方法:

下载文件到本地:
1、使用 Java 的 HttpURLConnection 或者 HttpClient 等工具来下载文件。
2、创建 FileInputStream:使用下载后的本地文件路径创建 FileInputStream。

java">public static void main(String[] args) throws Exception {String remoteUrl = "http://s3.api.com/diaoyun//survey/answer/.xlsx";String localPath = "E:\\project\\file11.xlsx"; // 本地临时文件路径downloadFileFromURL(remoteUrl, localPath);File file = new File(localPath);FileInputStream fileInputStream = new FileInputStream(file);Workbook workbook = WorkbookFactory.create(fileInputStream);
//        Workbook workbook = WorkbookFactory.create(new FileInputStream(new File("E:\\project\\工作簿1 - 副本.xls")));if (workbook instanceof POIXMLDocument) {POIXMLDocument poixmlDocument = (POIXMLDocument) workbook;POIXMLProperties properties = poixmlDocument.getProperties();POIXMLProperties.CoreProperties coreProperties = properties.getCoreProperties();// 现在你可以访问核心属性了String title = coreProperties.getTitle();String subject = coreProperties.getSubject();String creator = coreProperties.getCreator();// ... 其他属性// 打印属性到控制台System.out.println("Title: " + title);System.out.println("Subject: " + subject);System.out.println("Creator: " + creator);// ...} else {System.out.println("The workbook is not a POIXMLDocument (not an .xlsx file?).");}// 关闭工作簿(在try-with-resources中自动关闭fis,但这里显式关闭workbook以强调)workbook.close();file.delete();}private static void downloadFileFromURL(String urlStr, String localPath) throws IOException {URL url = new URL(urlStr);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();try (InputStream in = urlConnection.getInputStream();FileOutputStream out = new FileOutputStream(localPath)) {byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = in.read(buffer)) != -1) {out.write(buffer, 0, bytesRead);}}}

http://www.ppmy.cn/news/1540674.html

相关文章

electron-vite_11各平台 Electron 镜像存到哪里了?

建议设置了 NPM 镜像和 Electron 源&#xff1b;速度会快一点&#xff1b;electron-builder 在打包的时候&#xff0c;会根据系统的不同去各自的 NPM 缓存目录下查找对应版本的 Electron 源&#xff1b; 各操作系统对应的 NPM 缓存路径分别为&#xff1a; Linux: $XDG_CACHE_H…

六、存储过程和触发器及视图和临时表

一. 存储过程和触发器是数据库中用于实现复杂业务逻辑和自动化操作的重要工具。 下面是对存储过程和触发器的详细讲解和示例说明&#xff1a;存储过程&#xff1a; 存储过程是一组预定义的SQL语句&#xff0c;封装在数据库中并可通过名称调用。存储过程可以接受输入参数和输出…

【算法】深入理解布隆过滤器

1. 什么是布隆过滤器&#xff1f; 布隆过滤器&#xff08;Bloom Filter&#xff09;是一种空间效率极高的概率型数据结构&#xff0c;用于检测某个元素是否在一个集合中。与常见的数据结构如哈希表不同&#xff0c;布隆过滤器无法删除元素&#xff0c;并且会存在一定的误判率&…

市场上几个跨平台开发框架?

跨平台桌面应用开发框架是一种工具或框架&#xff0c;它允许开发者使用一种统一的代码库或语言来创建能够在多个操作系统上运行的桌面应用程序。传统上&#xff0c;开发者需要为每个操作系统编写不同的代码&#xff0c;使用不同的开发工具和语言。而跨平台桌面应用开发框架通过…

list(1)

list 大体上与之前学的string,vector类似&#xff0c;list不支持[]访问&#xff0c;擅长头插&#xff0c;头删&#xff0c;尾插&#xff0c;尾删&#xff0c;中间元素插入删除&#xff0c;因为list底层是双向循环带头链表 一段代码演示&#xff1a; #include <iostream>…

tftpd.exe开启调试

tftpd.exe开启调试 debugFlags设置为0xf开启debug 设置为0xf000f则开启debug和trace 第一部分&#xff1a; 位置 net/tcpip/services/tftpd/service.c if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tftpd\\Paramet…

【推导过程】常用离散分布的数学期望、方差、特征函数

文章目录 相关教程相关文献常用离散分布的数学期望&方差&特征函数二项分布数学期望方差 泊松分布泊松定理数学期望方差 超几何分布超几何分布的二项近似数学期望方差 几何分布几何分布的无记忆性数学期望方差 负二项分布 作者&#xff1a;小猪快跑 基础数学&计算数…

笔试练习day7

目录 OR59 字符串中找出连续最长的数字串题目解析解法(双指针遍历)代码 NC109 岛屿数量题目解析解法代码(dfs)dfs的实现 拼三角题目解析解法(枚举)代码 感谢各位大佬对我的支持,如果我的文章对你有用,欢迎点击以下链接 &#x1f412;&#x1f412;&#x1f412; 个人主页 &…