在页面上显示各种文档中的内容。在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. SSM登陆

    简单的SSM登陆 jsp <form action="${pageContext.request.contextPath }/user/login.action"> & ...

  2. switch case加范围判断

    switch case是可以加范围判断的,但是语法有少许变化,参数不能写在switch里面,而是写在外面,如: const i = 3; switch (true) { case (i <= 0 ...

  3. Photoshop CC 2020 (PS 2020)改变与新功能

    Adobe Photoshop CC 2020中文版新增相机防抖动功能.CameraRAW 功能改进.图像提升采样.属性面板改进.Behance集成等功能,以及同步设置和其他更多有用的功能.ps202 ...

  4. vs扩展和更新插件的开发

    一.调试 以 MinimalisticView.vsix (https://github.com/poma/MinimalisticView) 为例. 正如 | Marketplace 上介绍的,这个 ...

  5. npm install 报错 -4048

    方法1: 删除npmrc文件. 强调:不是nodejs安装目录npm模块下的那个npmrc文件,而是在C:\Users\{账户}\下的.npmrc文件. 方法2: https://www.jiansh ...

  6. Scrum冲刺第一篇

    一.各个成员在 Alpha 阶段认领的任务 负责人和协作者 任务内容 陈嘉欣 设计编码规范 邓镇港 UI设计 肖烈涛 数据库设计 林德泽 设计测试计划 余晓东 用户注册登陆验证模块 陈嘉欣 余晓东 林 ...

  7. 数字,字符串,time模块,文本进度条

    数字和字符串 数字类型 整形 整数, 1/2/3/12/2019 整形用来描述什么, 身高/年龄/体重 age = 18 height = 180 浮点型 浮点数,小数 salary = 10 pri ...

  8. unsigned int数据类型最大数

    #include <stdio.h> int main() { unsigned , b = ; ) { a++; } printf( ); unsigned ; do { n = n / ...

  9. 【买衣服】I'm looking for a jacket

    核心句型: I 'm looking for a jacket. 我想买一件夹克. 场景对话 A: Are you looking for anything? 您想买什么吗? B: Yes. I'm ...

  10. 201871010106-丁宣元 《面向对象程序设计(java)》第十一周学习总结

    201871010106-丁宣元 <面向对象程序设计(java)>第十一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...