当前位置: 首页 > news >正文

【总结】前端JQuery获取Java后端文件流实现常规附件预览功能

前端JQuery获取Java后端文件流实现常规附件预览功能

  • 项目背景
  • 1. Java后端处理附件
    • 1.1 将word文档转换为pdf格式
    • 1.2 将文件流返回前端
  • 2. 前端处理附件预览(JQuery)
    • 2.1 预览pdf文件
    • 2.2 预览ofd文件
    • 2.3 预览图片、txt文档
  • 共勉。

项目背景

目前维护的项目是个远古的SpringMVC项目,前端使用的是JQuery和HTML,与Vue相比维护比较复杂,功能实现上没有那么丰富。在做附件预览的需求时,考虑不借助第三方预览服务,通过开源组件实现,需要借助以下服务依赖:
1)Java:引入aspose.words依赖(支持将word文档转换为pdf格式)
注意:需要引入license

1. Java后端处理附件

1.1 将word文档转换为pdf格式

1)在resource下新增license.xml

<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

2)word转pdf工具类

/*** @program: Mickey* @description: pdf工具类**/
public class WordToPdfUtil {public static byte[] toPdfBytes(HttpServletRequest req, InputStream inputstream) throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] b = null;try {//认证aspose-word,否则转换的pdf会有水印if (!getWordLicense(req)) {return null;}LoadOptions options = new LoadOptions();options.setLoadFormat(LoadFormat.DOC);Document doc = new Document(inputstream, options);doc.save(bos, SaveFormat.PDF);b = bos.toByteArray();} catch (Exception e) {e.printStackTrace();throw new RuntimeException("word转换pdf出错!");} finally {bos.close();}return b;}/*** 获取aspose-word授权** @return*/public static boolean getWordLicense(HttpServletRequest req) {boolean result = false;InputStream is = null;try {//获取认证文件,并转为输入流is = new FileInputStream(req.getSession().getServletContext().getRealPath("") + File.separator + "downFile/pdfFile" + File.separator + "license.xml");//创建密钥认证对象License aposeLic = new License();//进行认证ssaposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
}

1.2 将文件流返回前端

try (InputStream in = new FileInputStream(new File("文件路径"))) {// 预览附件类型白名单List<String> whiteFileTypeList = Arrays.asList(DOC_SUFFIX, DOCX_SUFFIX, PDF_SUFFIX, TXT_SUFFIX, OFD_SUFFIX, PNG_SUFFIX, JPG_SUFFIX, JPEG_SUFFIX);// 不支持附件预览的类型if (StringUtils.isNotBlank(suffix) && !whiteFileTypeList.contains(suffix)) {throw new RuntimeException("不支持的附件预览类型,请下载后查看!");}String fileName = "预览附件名称";byte[] toPdfBytes;// word文档类型进行格式转换if (StringUtils.equals(DOC_SUFFIX, suffix) || StringUtils.equals(DOCX_SUFFIX, suffix)) {toPdfBytes = WordToPdfUtil.toPdfBytes(request, in);} else {toPdfBytes = readInputStream(in);if (StringUtils.equals(PNG_SUFFIX, suffix) || StringUtils.equals(JPG_SUFFIX, suffix) || StringUtils.equals(JPEG_SUFFIX, suffix)) {String base64Str = Base64Utils.encodeToString(toPdfBytes);model.put("base64Str", base64Str);}if (StringUtils.equals(TXT_SUFFIX, suffix)) {model.put("txtView", toPdfBytes);}}if (toPdfBytes != null) {try {String contextPath = request.getContextPath();String basePath = request.getSession().getServletContext().getRealPath(File.separator);File outFile = new File(basePath + File.separator + "temp" + File.separator + "fileView");if (!outFile.isDirectory()) {if (!outFile.exists()) {boolean b = outFile.mkdirs();}}FileOutputStream fo = new FileOutputStream(outFile + File.separator + "附件id" + ".pdf");BufferedOutputStream out = new BufferedOutputStream(fo);out.write(toPdfBytes, 0, toPdfBytes.length);out.flush();out.close();return contextPath + File.separator + "temp" + File.separator + "fileView" + File.separator + "附件id" + ".pdf";} catch (IOException e) {e.printStackTrace();}}
} catch (IOException e) {log.error("附件处理异常", e);
} 

2. 前端处理附件预览(JQuery)

2.1 预览pdf文件

引入PDFView组件

<iframe id="pdfFrame" src="${base}/pdfView/web/viewer.html?file=${viewFilePath}" width="900" height="530"></iframe>

2.2 预览ofd文件

引入ofd.js依赖,支持ofd文件查看

<div class="ofdContainer" id="ofdContainer" style="width: 900px; height: 530px; overflow-y: scroll;"></div>
$(function() {// ofd文件预览if (suffix && suffix === 'ofd') {var xhr = new XMLHttpRequest();xhr.open('POST', '附件下载地址', true);xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');xhr.responseType = 'blob';xhr.send("id=" + 附件id);xhr.onload = function(e) {if (this.status === 200) {var blob = new Blob([this.response], {type: 'application/ofd'});// 处理文件流,比如使用 FileReader 进行读取或者直接下载const file = new File([blob], fileName + '.ofd', { type: blob.type });ofd.parseOfdDocument({ofd: file,success: function (res) {const screenWidth = 800;const ofdRenderRes = ofd.renderOfd(screenWidth, res[0]);let ofdContainerDiv = document.getElementById('ofdContainer');// 清空元素ofdContainerDiv.innerHTML = '';for (const item of ofdRenderRes) {ofdContainerDiv.appendChild(item);}},fail: function (err) {console.error(err);},});}};}
})

2.3 预览图片、txt文档

1)将后端获取的文件流转换为Base64格式,前端展示图片

<img src="data:image/jpeg;base64,${base64Str!}" style="width: 900px; height: 530px; overflow-y: scroll;" />

2)展示txt文档

<pre class="txtContainer" id="txtContainer" style="width: 900px; height: 530px; overflow-y: scroll; text-align: left;"></pre>
fetch('替换为你的后端TXT文件下载地址' + 附件id).then(response => response.text()).then(text => {document.getElementById('txtContainer').textContent = text;}).catch(error => console.error('读取文件出错:', error));

共勉。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Qt基础 | QSqlQueryModel 的使用 | QSqlQuery的使用
  • 深入理解计算机系统 CSAPP 家庭作业11.7
  • SMA 内孔 弯头——KH-SMA-K513-G
  • SQL进阶:解锁高级特性,深化数据洞察
  • MySQL零散拾遗(七)--- 突发奇想的一些疑虑
  • 力扣SQL50 上级经理已离职的公司员工 一题双解
  • Python | Leetcode Python题解之第283题移动零
  • 按图搜索新体验:阿里巴巴拍立淘API返回值详解
  • 代码实践思考:ROS1和ROS2
  • 反转链表 - 力扣(LeetCode)C语言
  • 成为git砖家(2): gitk 介绍
  • 模拟实现c++中的string
  • C# 知识点总结
  • 前端了解到框架-网络复习
  • Unity Canvas动画:UI元素的动态展示
  • 深入了解以太坊
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • CentOS 7 防火墙操作
  • Create React App 使用
  • DOM的那些事
  • EOS是什么
  • idea + plantuml 画流程图
  • PHP变量
  • 测试开发系类之接口自动化测试
  • 从0到1:PostCSS 插件开发最佳实践
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 如何设计一个微型分布式架构?
  • 智能网联汽车信息安全
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • 说说我为什么看好Spring Cloud Alibaba
  • # 飞书APP集成平台-数字化落地
  • #if #elif #endif
  • $ git push -u origin master 推送到远程库出错
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (1)(1.13) SiK无线电高级配置(五)
  • (11)MATLAB PCA+SVM 人脸识别
  • (Forward) Music Player: From UI Proposal to Code
  • (vue)el-cascader级联选择器按勾选的顺序传值,摆脱层级约束
  • (六)vue-router+UI组件库
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .gitignore文件_Git:.gitignore
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .net core控制台应用程序初识
  • .Net OpenCVSharp生成灰度图和二值图
  • .NET Remoting学习笔记(三)信道
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .Net 知识杂记
  • .NET 中 GetProcess 相关方法的性能
  • .NET 中的轻量级线程安全