在页面上显示各种文档中的内容。在servlet中的逻辑

word:

BufferedInputStream bis = null;
  URL url = null;
  HttpURLConnection httpUrl = null; // 建立链接
  url = new URL(urlReal);
  httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
  httpUrl.connect();// 获取网络输入流
  bis = new BufferedInputStream(httpUrl.getInputStream());

String bodyText = null;
  WordExtractor ex = new WordExtractor(bis);
  bodyText = ex.getText();
  response.getWriter().write(bodyText);

excel:

BufferedInputStream bis = null;
  URL url = null;
  HttpURLConnection httpUrl = null; // 建立链接
  url = new URL(urlReal);
  httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
  httpUrl.connect();// 获取网络输入流
  bis = new BufferedInputStream(httpUrl.getInputStream());

content = new StringBuffer();
  HSSFWorkbook workbook = new HSSFWorkbook(bis);
  for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
   HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
   content.append("/n");
   if (null == aSheet) {
    continue;
   }
   for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {
    content.append("/n");
    HSSFRow aRow = aSheet.getRow(rowNum);
    if (null == aRow) {
     continue;
    }
    for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {
     HSSFCell aCell = aRow.getCell(cellNum);
     if (null == aCell) {
      continue;
     }
     if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
      content.append(aCell.getRichStringCellValue()
        .getString());
     } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
      boolean b = HSSFDateUtil.isCellDateFormatted(aCell);
      if (b) {
       Date date = aCell.getDateCellValue();
       SimpleDateFormat df = new SimpleDateFormat(
         "yyyy-MM-dd");
       content.append(df.format(date));
      }
     }
    }
   }
  }
  response.getWriter().write(content.toString());

ppt:

BufferedInputStream bis = null;
  URL url = null;
  HttpURLConnection httpUrl = null; // 建立链接
  url = new URL(urlReal);
  httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
  httpUrl.connect();// 获取网络输入流
  bis = new BufferedInputStream(httpUrl.getInputStream());

StringBuffer content = new StringBuffer("");
  SlideShow ss = new SlideShow(new HSLFSlideShow(bis));
  Slide[] slides = ss.getSlides();
  for (int i = 0; i < slides.length; i++) {
   TextRun[] t = slides[i].getTextRuns();
   for (int j = 0; j < t.length; j++) {
    content.append(t[j].getText());
   }
   content.append(slides[i].getTitle());
  }
  response.getWriter().write(content.toString());

pdf:

BufferedInputStream bis = null;
  URL url = null;
  HttpURLConnection httpUrl = null; // 建立链接
  url = new URL(urlReal);
  httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
  httpUrl.connect();// 获取网络输入流
  bis = new BufferedInputStream(httpUrl.getInputStream());

PDDocument pdfdocument = null;
  PDFParser parser = new PDFParser(bis);
  parser.parse();
  pdfdocument = parser.getPDDocument();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  OutputStreamWriter writer = new OutputStreamWriter(out);
  PDFTextStripper stripper = new PDFTextStripper();
  stripper.writeText(pdfdocument.getDocument(), writer);
  writer.close();
  byte[] contents = out.toByteArray();

String ts = new String(contents);
  response.getWriter().write(ts);

txt:

BufferedReader bis = null;
  URL url = null;
  HttpURLConnection httpUrl = null; // 建立链接
  url = new URL(urlReal);
  httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
  httpUrl.connect();// 获取网络输入流
  bis = new BufferedReader( new InputStreamReader(httpUrl.getInputStream()));

StringBuffer buf=new StringBuffer();
  String temp;
  while ((temp = bis.readLine()) != null) {
   buf.append(temp);
   response.getWriter().write(temp);
   if(buf.length()>=1000){
    break;
   }
  }
  bis.close();
————————————————

apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码的更多相关文章

  1. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  2. java在线预览txt、word、ppt、execel,pdf代码

    在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputStream bis = null; URL url = null; HttpURLConnection ...

  3. 怎么在线预览.doc,.docx,.ofd,.pdf,.wps,.cad文件以及Office文档的在线解析方式。

    前言 Office文件在线预览是目前移动化办公的一种新趋势.Office在线预览指的是Office系列的文件在线查看而不依附域客户端的存在.在浏览器或者浏览器控件中可以预览查看Word.PDF.Exc ...

  4. ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...

  5. [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...

  6. 文件在线预览doc,docx转换pdf(一)

    文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...

  7. 使用微软提供的Office Online实现Office文档的在线查看,编辑等功能

    使用微软提供的Office Online平台只需要一个网址即可在线查看Xls,doc,PPT等文档 http://view.officeapps.live.com/op/view.aspx?src=要 ...

  8. 实战动态PDF在线预览及带签名的PDF文件转换

    开篇语: 最近工作需要做一个借款合同,公司以前的合同都是通过app端下载,然后通过本地打开pdf文件,而喜欢创新的我,心想着为什么不能在线H5预览,正是这个想法,说干就干,实践过程总是艰难的,折腾了3 ...

  9. 前端实现文件在线预览txt,pdf,doc,xls,ppt几种格式

    做法就是使用iframe标签 1.text,pdf的文件预览 <iframe class="filename" :src="文件的地址" width='1 ...

随机推荐

  1. Vue.js项目实战-多语种网站(租车)

    首先来看一下网站效果,想写这个项目的读者可以自行下载哦,地址:https://github.com/Stray-Kite/Car: 在这个项目中,我们主要是为了学习语种切换,也就是右上角的 中文/En ...

  2. IntelliJ IDEA 中使用 Lambok (注解无效问题的解决)

    一,概述 Lambok可以说一个能很大提高开发效率的插件,只要在使用注解的方式就能实现很多常用的功能.如常用的@Data能在编译阶段自动生成toString方法,getter方法setter方法等. ...

  3. UILabel的各种属性和方法

    转自:http://liulu200888an.blog.163.com/blog/static/3498972320121214208542/ UILabel  *label1 = [[UILabe ...

  4. 如何在浏览器中运行 VS Code?

    摘要: WEB IDE新时代! 作者:SHUHARI 的博客 原文:有趣的项目 - 在浏览器中运行 Visual Studio Code Fundebug按照原文要求转载,版权归原作者所有. 众所周知 ...

  5. CentOS7 配置 SSH监听多个端口方法

    一.修改ssh默认端口,防止暴力破解,让系统安全多一点点: i. 在配置文件/etc/ssh/sshd_config文件中修改 Port #AddressFamily any #ListenAddre ...

  6. 《SQL Server 2008 R2》 收缩数据库日志文件

    USE [master] GO /****** Object: StoredProcedure [dbo].[pro_Shrink_Log] Script Date: 2019/8/16 16:56: ...

  7. VUE注册局部组件

    // 局部组件命名规范 /* 1文件夹名大驼峰 MyLocalBtn.vue 2 使用的时候 将驼峰转化为横杠 <my-local-btn></my-local-btn> */ ...

  8. 03导航链接的制作(wx:for循环)和小程序警告request fail url not in domain list

    06==>导航链接的制作 <!-- 导航链接 --> <navigator url="../list/list" hover-class="nav ...

  9. mysql的创建数据库表及添加数据

    C:\Users\ceshi>mysql -u root -pEnter password: ******Welcome to the MySQL monitor. Commands end w ...

  10. uiautomator 调试例子

    package com.bing.cn; import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class Dem ...