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

网站首页设计公司/关键词百度云

网站首页设计公司,关键词百度云,网站建设文字资料,网页设计与制作软件下载点击关注公众号,Java干货及时送达Java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务,不过需要收费 如果想要免费的,可以用openoffice,实现原理就是:通过第三方…

点击关注公众号,Java干货及时送达1677c9e8055752e82d6936ca46ef504e.png

Java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务,不过需要收费 如果想要免费的,可以用openoffice,实现原理就是:

通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件流;

当然如果装了Adobe Reader XI,那把pdf直接拖到浏览器页面就可以直接打开预览,前提就是浏览器支持pdf文件浏览。

我这里介绍通过poi实现word、excel、ppt转pdf流,这样就可以在浏览器上实现预览了。另外,Java 面试题和答案全部整理好了,微信搜索Java技术栈,在后台发送:面试,可以在线阅读。

1.到官网下载Apache OpenOffice 安装包,安装运行。

不同系统的安装方法,自行百度,这里不做过多说明。

2c10ba96555d5fccd09be3d100213fe1.png

2.再项目的pom文件中引入依赖

<!--openoffice-->
<dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.1</version>
</dependency>

3.将word、excel、ppt转换为pdf流的工具类代码

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;/*** 文件格式转换工具类** @author tarzan* @version 1.0* @since JDK1.8*/
public class FileConvertUtil {/** 默认转换后文件后缀 */private static final String DEFAULT_SUFFIX = "pdf";/** openoffice_port */private static final Integer OPENOFFICE_PORT = 8100;/*** 方法描述 office文档转换为PDF(处理本地文件)** @param sourcePath 源文件路径* @param suffix     源文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {File inputFile = new File(sourcePath);InputStream inputStream = new FileInputStream(inputFile);return covertCommonByStream(inputStream, suffix);}/*** 方法描述  office文档转换为PDF(处理网络文件)** @param netFileUrl 网络文件路径* @param suffix     文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {// 创建URLURL url = new URL(netFileUrl);// 试图连接并取得返回状态码URLConnection urlconn = url.openConnection();urlconn.connect();HttpURLConnection httpconn = (HttpURLConnection) urlconn;int httpResult = httpconn.getResponseCode();if (httpResult == HttpURLConnection.HTTP_OK) {InputStream inputStream = urlconn.getInputStream();return covertCommonByStream(inputStream, suffix);}return null;}/*** 方法描述  将文件以流的形式转换** @param inputStream 源文件输入流* @param suffix      源文件后缀* @return InputStream 转换后文件输入流* @author tarzan*/public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {ByteArrayOutputStream out = new ByteArrayOutputStream();OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);connection.connect();DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);converter.convert(inputStream, sourceFormat, out, targetFormat);connection.disconnect();return outputStreamConvertInputStream(out);}/*** 方法描述 outputStream转inputStream** @author tarzan*/public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {ByteArrayOutputStream baos=(ByteArrayOutputStream) out;return new ByteArrayInputStream(baos.toByteArray());}public static void main(String[] args) throws IOException {//convertNetFile("http://172.16.10.21/files/home/upload/department/base/201912090541573c6abdf2394d4ae3b7049dcee456d4f7.doc", ".pdf");//convert("c:/Users/admin/Desktop/2.pdf", "c:/Users/admin/Desktop/3.pdf");}
}

4.serve层在线预览方法代码

/*** @Description:系统文件在线预览接口* @Author: tarzan*/
public void onlinePreview(String url, HttpServletResponse response) throws Exception {//获取文件类型String[] str = SmartStringUtil.split(url,"\\.");if(str.length==0){throw new Exception("文件格式不正确");}String suffix = str[str.length-1];if(!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")&& !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")){throw new Exception("文件格式不支持预览");}InputStream in=FileConvertUtil.convertNetFile(url,suffix);OutputStream outputStream = response.getOutputStream();//创建存放文件内容的数组byte[] buff =new byte[1024];//所读取的内容使用n来接收int n;//当没有读取完时,继续读取,循环while((n=in.read(buff))!=-1){//将字节数组的数据全部写入到输出流中outputStream.write(buff,0,n);}//强制将缓存区的数据进行输出outputStream.flush();//关流outputStream.close();in.close();
}

5.controler层代码

@ApiOperation(value = "系统文件在线预览接口 by tarzan")
@PostMapping("/api/file/onlinePreview")
public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{fileService.onlinePreview(url,response);
}

原文链接:https://blog.csdn.net/weixin_40986713/article/details/109527294

版权声明:本文为CSDN博主「洛阳泰山」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

3787c7aa97bdf18f8118c601662bf05a.gif

696695b89224928584dfdf7cb7c5071b.png

5713ea0efa8cf12bdfba2e74428968dd.png

1f282a323c15811e3232c839f6858207.png

e30a745d540fde98c5261a76db62f2ac.png

5d5ce5c89d067605ca663655b078b5e6.png

869bbeec2c583b3dab6823c5126fdc69.png

b9774ae6939716fa1ba120b157e17048.png

关注Java技术栈看更多干货

5be74604d4703d1e03f9c57214b8eff1.png

7efb39a0bb52b12dd445b167fa43f995.gif

获取 Spring Boot 实战笔记!

http://www.lbrq.cn/news/1031059.html

相关文章:

  • 为什么要做企业网站/博为峰软件测试培训学费
  • 自己有服务器怎么建设网站/个人博客网页制作
  • 网页设计怎么做网站/网络宣传方式有哪些
  • 百度推广自己做网站/营销策略有哪些有效手段
  • 如何粘贴网站统计代码/谷歌浏览器手机版
  • 网站建设 国外/什么平台可以发广告引流
  • 上海注册公司电话咨询/网站优化seo是什么意思
  • c 能用来做网站吗/企业网站管理系统
  • 推广型网站建设机构/网站优化北京seo
  • icp网站备案管理系统/常用的网络推广的方法有哪些
  • 如何建网站遂宁/seo快速排名软件
  • 企业网站营销网站/免费网页制作成品
  • 如何做好政府网站建设/壹起航网络推广的目标
  • 西充县住房和城乡建设局网站/百度网站安全检测
  • 光谷软件园网站建设/云南网络推广公司排名
  • 响应式表白网站源码/成品网站建站空间
  • 网页首页动态设计/哈尔滨百度搜索排名优化
  • 网站 标签导航/无锡网站建设方案优化
  • 做网站公司好开吗/广告最多的网站
  • 做外贸怎样浏览国外网站/接app推广
  • 深圳网站建设交易/seo实战技术培训
  • 与网站开发相关的书籍/学电脑培训班
  • 河南国安建设集团有限公司网站/湖南网站建设推荐
  • easyui 网站开发实现/厦门百度广告
  • 旅游主题网站怎么做/磁力蜘蛛搜索引擎
  • 申请个人网站怎么申请/百度推广电话销售好做吗
  • 网站建设项目策划/网址域名大全
  • 免费手机网页网站/友情链接管理系统
  • 深圳企业网站建设公司哪家好/百度图片识别在线识图
  • 软件开发流程详解/西安seo专员
  • 利用 Python 爬虫按图搜索 1688 商品(拍立淘)实战指南
  • 深度学习·ExCEL
  • 长篇音频制作(小说自动配音)完整教程
  • 嵌入式系统学习Day17(文件编程-库函数调用)
  • Java进阶学习之不可变集合
  • 【Java Web 快速入门】九、事务管理