office文件转pdf在线预览

一、工具类

package com.sby.utils;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Locale;import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;/*** Created with IntelliJ IDEA.** @Author: cqwuliu* @Date: 2024/02/08/11:41  will_isme@163.com* @Description:*/
public class AsposeUtil {/*** 获取license** @return*/public static boolean getLicense(int type) {boolean result = false;try {InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");if (type == 1) {//excelcom.aspose.cells.License aposeLic = new com.aspose.cells.License();aposeLic.setLicense(is);result = true;} else if (type == 2) {//wordcom.aspose.words.License aposeLic = new com.aspose.words.License();aposeLic.setLicense(is);result = true;} else {//pptcom.aspose.slides.License aposeLic = new com.aspose.slides.License();aposeLic.setLicense(is);result = true;}} catch (Exception e) {e.printStackTrace();}return result;}public static String Excel2Pdf(String officePath,String OutPutPath,String officeName) {// 验证Licenseif (!getLicense(1)) {return null;}try {File file = new File(OutPutPath);if (!file.exists()) {file.mkdirs();}
//            long old = Sysout.currentTimeMillis();Workbook wb = new Workbook(officePath);// 原始excel路径File pdfFile = new File(OutPutPath+officeName);// 输出路径FileOutputStream fileOS = new FileOutputStream(pdfFile);//wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);wb.save(fileOS, pdfSaveOptions);return pdfFile.getAbsolutePath();
//            long now = Sysout.currentTimeMillis();
//            Sysout.println("共耗时:" + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();System.err.println("Excel2Pdf转换pdf错误");}return null;}public static String Word2Pdf(String officePath,String OutPutPath,String officeName) {// 验证Licenseif (!getLicense(2)) {return null;}try {File file = new File(OutPutPath);if (!file.exists()) {file.mkdirs();}Document doc = new Document(officePath);// 原始word路径File pdfFile = new File(OutPutPath+officeName);// 输出路径FileOutputStream fileOS = new FileOutputStream(pdfFile);doc.save(fileOS, SaveFormat.PDF);return pdfFile.getAbsolutePath();} catch (Exception e) {e.printStackTrace();System.err.println("word转换pdf错误");}return null;}public static String PPT2Pdf(String officePath,String OutPutPath,String officeName) {// 验证Licenseif (!getLicense(3)) {return null;}try {File PathFile = new File(OutPutPath);if (!PathFile.exists()) {PathFile.mkdirs();}InputStream slides = new FileInputStream(new File(officePath));// 原始ppt路径Presentation pres = new Presentation(slides);File file = new File(OutPutPath+officeName);// 输出pdf路径FileOutputStream fileOS = new FileOutputStream(file);pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);return file.getAbsolutePath();} catch (Exception e) {e.printStackTrace();System.err.println("ppt转换pdf错误");}return null;}/*** fileTyle.equals(".DOCX") || fileTyle.equals(".DOC") || fileTyle.equals(".PPT") || fileTyle.equals(".PPTX") || fileTyle.equals(".XLS") || fileTyle.equals(".XLSX")* @param officePath* @return 返回转换以后的pdf文件路径*/public static String OfficeToPdf(String officePath) {//G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docx⌒101951.docx⌒feiyu.docxString[] split = officePath.split("⌒");int lastIndex = split[0].lastIndexOf(".");int lastNameIndex = split[0].lastIndexOf("\\");String officeType = split[0].substring(lastIndex+1).toLowerCase(Locale.ROOT);String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf";String OutPutPath = split[0].substring(0,lastNameIndex+1)+"topdf/";File file = new File(split[0]);File pdfFile = new File(OutPutPath+officeName);//判断当前office文件是否已经转为PDF,如果已转为PDF就不需要再次转换。if(pdfFile.exists()){return OutPutPath+officeName;}if (file.exists()) {double bytes = file.length();double kilobytes = (bytes / 1024);double megabytes = (kilobytes / 1024);DecimalFormat df = new DecimalFormat("0.00");df.setRoundingMode(RoundingMode.HALF_UP);String MB = df.format(megabytes);Double Size = Double.parseDouble(MB);if(Size>30){return Size+"MB";}//"doc", "docx", "xls","xlsx", "ppt", "pptx"try {if(officeType.equals("doc")||officeType.equals("docx")){return   Word2Pdf(split[0],OutPutPath,officeName);}else if(officeType.equals("xls")||officeType.equals("xlsx")){return   Excel2Pdf(split[0],OutPutPath,officeName);}else if(officeType.equals("ppt")||officeType.equals("pptx")){return   PPT2Pdf(split[0],OutPutPath,officeName);}else{System.err.println("无法识别该文件");return "Error";}} catch (Exception e) {e.printStackTrace();}} else {return "NotExists";}return OutPutPath+officeName;}// public static void main(String[] args) {//  OfficeToPdf("C:\\Users\\DELL\\Desktop\\桌面表格文件\\2020年沙坪坝维护.xlsx");// }}相关jar包在我的资源里面下载

调用方法将不同文件类型分类处理后发送到前端预览,不在分类中的直接发送文件下载。

 public void previewFile(Integer id, HttpServletResponse response) throws IOException {FileInfo files = fileMapper.getFilesById(id);String filePate = dir + "\\" + files.getAliasName();String fileName = files.getAliasName();String fileTyle = fileName.substring(fileName.lastIndexOf("."), fileName.length()).toUpperCase();FileInputStream fileInputStream = new FileInputStream(filePate);int available = fileInputStream.available();if (available>(1024*1024*readonline) || fileTyle.equals(".DOCX") || fileTyle.equals(".DOC") || fileTyle.equals(".PPT") || fileTyle.equals(".PPTX") || fileTyle.equals(".XLS") || fileTyle.equals(".XLSX")) {try{//将office转换成pdf "预览";fileInputStream = new FileInputStream(AsposeUtil.OfficeToPdf(filePate));IOUtils.copy(fileInputStream, response.getOutputStream());return;}catch (Exception e){log.error("officez转换pdf预览失败"+filePate);System.err.println("officez转换pdf预览失败");}String filename = files.getFileName();filename = java.net.URLEncoder.encode(filename, "UTF-8").replace("+", "%20");response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);IOUtils.copy(fileInputStream, response.getOutputStream());} else if (!fileTyle.equals(".PNG") && !fileTyle.equals(".JPG") && !fileTyle.equals(".JPEG") && !fileTyle.equals(".PDF")) {BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));String line;while ((line = br.readLine()) != null) {response.setContentType("text/html;charset=UTF-8");response.getWriter().write(line);response.getWriter().write("<br/>");}} else {IOUtils.copy(fileInputStream, response.getOutputStream());}}<dependency><groupId>com.aspose</groupId><artifactId>aspose.slides</artifactId><version>15.9.0</version><scope>system</scope><systemPath>${basedir}/lib/aspose.slides-15.9.0.jar</systemPath></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose.cells.java</artifactId><version>18.11</version><scope>system</scope><systemPath>${basedir}/lib/aspose-cells-java-18.11.jar</systemPath></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose.words</artifactId><version>15.8.0</version><scope>system</scope><systemPath>${basedir}/lib/aspose-words-15.8.0.jar</systemPath></dependency>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/676215.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Web】vulhub Shiro-550反序列化漏洞复现学习笔记

目录 Shiro简介 复现流程 工具一把梭 半脚本半手动 原理分析 反序列化入口 常见的key 登录过程 验证过程 利用原理 Shiro简介 Apache Shiro 是一个强大且易于使用的 Java 安全框架&#xff0c;用于身份验证、授权、加密和会话管理等安全功能。Shiro 的设计目标是简单…

【Spring】springmvc如何处理接受http请求

目录 ​编辑 1. 背景 2. web项目和非web项目 3. 环境准备 4. 分析链路 5. 总结 1. 背景 今天开了一篇文章“SpringMVC是如何将不同的Request路由到不同Controller中的&#xff1f;”&#xff1b;看完之后突然想到&#xff0c;在请求走到mvc 之前服务是怎么知道有请求进来…

大模型是如何实现Function Call函数调用的?

▼最近直播超级多&#xff0c;预约保你有收获 近期直播&#xff1a;《Agent 企业级应用案例实战》 —1— 大模型如何实现函数调用&#xff1f; 大模型要实现精确的函数调用&#xff08;Function Call&#xff09;需要理解能力和逻辑能力&#xff0c;理解能力就是对用户的 Prom…

redhat grub.cfg配置文件丢失或报错解决

1.实验环境&#xff1a;把grub.cfg删除 [rootexample ~]# rm -rf /boot/grub2/grub.cfg 2.重启服务器 3&#xff0c;发现进入系统失败 输入以下命令 ls: 列出当前设备上的文件和目录。 grub> ls (hd0) (hd0,msdos3) (hd0,msd0s2) (hd0,msdos1) #一般第一个为/boot分区se…

技术精英求职必备:Java开发工程师简历制作全指南

投简历找工作嘛&#xff0c;这事儿其实就跟相亲差不多&#xff0c;得让对方一眼就看上你。 在这场职场的‘相亲’中&#xff0c;怎样才能让你的简历脱颖而出&#xff0c;成为HR眼中的理想‘对象’呢&#xff1f;来&#xff0c;我给你支几招&#xff0c;让你的简历更吸引人。 …

HiveSQL——用户中两人一定认识的组合数

注&#xff1a;参考文章&#xff1a; SQL之用户中两人一定认识的组合数--HQL面试题36【快手数仓面试题】_sql面试题-快手-CSDN博客文章浏览阅读1.2k次&#xff0c;点赞3次&#xff0c;收藏12次。目录0 需求分析1 数据准备2 数据分析3 小结0 需求分析设表名&#xff1a;table0现…

MPLS VPN功能组件(3)

私网标签分配 通过MPBGP为VPNv4路由分配内层标签 PE从CE接收到IPv4路由后&#xff0c;对该路由加上相应VRF的RD&#xff08;RD手动配置&#xff09;&#xff0c;使其成为一条VPNV4路由&#xff0c;然后在路由通告中更改下一跳属性为自己&#xff0c;通常是自己的Loopback地址…

sql实现将某一列下移一行

问题 实现如下图所示的 max_salary 下移一行 方法&#xff1a;使用开窗函数 select max_salary, max(max_salary) over(order by max_salary asc rows between 1 PRECEDING and 1 PRECEDING) max_salary_plus from jobs

containerd中文翻译系列(二十一)用户命名空间

支持用户命名空间 Kubernetes 自 v1.25 起支持使用用户命名空间运行 pod。本文档解释了 containerd 对该功能的支持。 什么是用户命名空间&#xff1f; 用户命名空间将容器内运行的用户与主机内的用户隔离开来。 在容器中以 root 用户身份运行的进程可以在主机中以不同的&a…

推荐一个支持微信、支付宝支付的升级chatgpt4的虚拟信用卡渠道

最新的充值方法看这里。 通过虚拟卡 WildCard 的方式来升级 GPT 4.0 最快了&#xff0c;大概2分钟就可以升级完成, 而且升级 GPT 4.0 价钱也不贵&#xff0c;虚拟卡一年10美元&#xff0c;GPT4 每个月也才 20美元。如果你觉得 GPT 4.0 对你可能有帮助&#xff0c;那就赶快来升级…

【ArcGIS微课1000例】0102:面状要素空洞填充

文章目录 一、实验描述二、实验数据三、实验步骤1. 手动补全空洞2. 批量补全空洞四、注意事项一、实验描述 在对地理数据进行编辑时,时常会遇到面数据中存在个别或大量的空洞,考虑实际情况中空洞的数量多少、分布情况,填充空洞区域可以采用逐个填充的方式,也可以采用快速大…

Qlik Sense : Lookup函数

LookUp - 脚本函数 Lookup() 用于查找已经加载的表格&#xff0c;并返回与在字段 match_field_name 中第一次出现的值 match_field_value 对应的 field_name 值。表格可以是当前表格或之前加载的其他表格。 语法&#xff1a; lookup(field_name, match_field_name, match_…

【工具】Android|Android Studio 长颈鹿版本安装下载使用详解

版本&#xff1a;2022.3.1.22&#xff0c; https://redirector.gvt1.com/edgedl/android/studio/install/2022.3.1.22/android-studio-2022.3.1.22-windows.exe 前言 笔者曾多次安装并卸载Android Studio&#xff0c;反复被安卓模拟器劝退。现在差不多是第三次安装&#xff0c…

Linux操作系统基础(五):Linux的目录结构

文章目录 Linux的目录结构 一、Linux目录与Windows目录区别 二、常见目录介绍&#xff08;记住重点&#xff09; Linux的目录结构 一、Linux目录与Windows目录区别 Linux的目录结构是一个树型结构 Windows 系统 可以拥有多个盘符, 如 C盘、D盘、E盘 Linux 没有盘符 这个概…

EasyRecovery免费版2024电脑数据恢复利器

在数字化时代&#xff0c;我们的生活和工作都离不开电脑&#xff0c;电脑硬盘中的数据却时常面临丢失的风险&#xff0c;无论是因为误删除、格式化、病毒感染还是硬件故障&#xff0c;都可能让我们付出沉重的代价&#xff0c;在这种情况下&#xff0c;一款强大的数据恢复软件就…

【力扣】复写零,栈 + 双指针法

复写零原题地址 方法一&#xff1a;双指针法 从前向后复写&#xff0c;会造成覆盖。所以&#xff0c;应该从后向前复写&#xff0c;这样我们可以考虑维护一个栈。遍历数组&#xff0c;如果遇到非零元素&#xff0c;就入栈一次&#xff1b;如果遇到零&#xff0c;就入栈两次。…

玉米基因miRNA结合位点预测工具

前记 目前&#xff0c;已经有很多种玉米miRNA结合位点预测工具可供选择&#xff0c;以下几种比较常用&#xff1a; 1、psRNATarget&#xff1a;该工具是由华盛顿州立大学开发的&#xff0c;可以用来预测植物miRNA和靶基因之间的相互作用。用户可以使用该工具来预测玉米miRNA和结…

Linux cp命令文档(cp指令)(cp --help)(各选项参数用法解释)

文章目录 cp命令文档英文中文 关键点示例-x, --one-file-system&#xff08;保持在此文件系统上&#xff0c;不做跨域文件系统拷贝&#xff09;含义示例 疑问什么是“长选项的强制参数对短选项也是强制的”&#xff1f;什么是“稀疏文件”&#xff1f; cp命令文档 英文 rootu…

【5G NR】移动通讯中使用的信道编解码技术

目录 一、引言 二、信道编解码技术概述 三、移动通讯中常用的信道编解码技术 四、优缺点分析与比较 五、未来发展趋势 六、结论 本文主要介绍了移动通讯中采用的信道编解码技术&#xff0c;由于在5G NR终端中&#xff0c;通常要兼容4G LTE通讯技术&#xff0c;所以4G LTE…

论文摘要和总结在某种程度上有相似之处,但它们在内容和目的上有所不同

论文摘要和总结在某种程度上有相似之处&#xff0c;但它们在内容和目的上有所不同。 1. **摘要 (Abstract)**: - 摘要通常位于论文开头&#xff0c;是对整篇论文的简要概述&#xff0c;其主要目的是向读者提供论文的核心内容、研究方法、主要结果和结论&#xff0c;以及研究…