博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java在线预览txt、word、ppt、execel,pdf代码
阅读量:5943 次
发布时间:2019-06-19

本文共 3597 字,大约阅读时间需要 11 分钟。

在页面上显示各种文档中的内容。在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();

 

转载于:https://www.cnblogs.com/vipplus/p/3683178.html

你可能感兴趣的文章
linux虚拟文件系统浅析
查看>>
HBase数据压缩编码探索
查看>>
sprint计划会议总结
查看>>
团队项目冲刺1
查看>>
fon循环总是返回最后值问题
查看>>
Android新权限机制 AppOps
查看>>
“蓝桥杯”软件大赛入门训练4道题
查看>>
[2010山东ACM省赛] Greatest Number(数的组合+二分搜索)
查看>>
Unable to get the CMake version located at
查看>>
爬虫基本原理
查看>>
Heritage from father
查看>>
css选择器
查看>>
使用多线程
查看>>
Linux-gate.so.1的含义[ZZ]
查看>>
Call指令和Ret指令讲解
查看>>
利用GetPrivateProfileString读取配置文件(.ini)
查看>>
Django--Uploaded Files以及Handlers
查看>>
请求一个action,将图片的二进制字节字符串在视图页面以图片形式输出
查看>>
android 颜色值参考,(有颜色图
查看>>
在IIS(64位)上部署WCF服务访问Oracle数据库
查看>>