1. 程式人生 > >java線上預覽txt、word、ppt、execel,pdf程式碼

java線上預覽txt、word、ppt、execel,pdf程式碼

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();

相關推薦

java線上txtwordpptexecelpdf程式碼

word: BufferedInputStream bis = null;   URL url = null;   HttpURLConnection httpUrl = null; // 建立連結   url = new URL(urlReal);   httpUrl = (HttpURLConnecti

apache poi操作office文件----java線上txtwordpptexecelpdf程式碼

在頁面上顯示各種文件中的內容。在servlet中的邏輯 word:  BufferedInputStream bis = null;   URL url = null;   HttpURLConnection httpUrl = null; // 建立連結   u

C#中mvc模式線上Office(word 可編輯txt)檔案

  /// <summary> /// 線上預覽Office檔案 /// </summary> public class OfficeViewController : Controller { #reg

java實現線上--poi實現word excel ppt轉html

                        java實現線上

java實現線上--poi實現wordexcelppt轉html

分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow 也歡迎轉載我的文章,轉載請註明出處 https://blog.csdn.net/aabbyyz java實現線上預覽 - -之poi實現word、e

java線上pdf檔案或圖片檔案

在專案中需要使用到線上瀏覽檔案功能,由於專案中只能上傳pdf和圖片檔案,所有就只做了預覽pdf和圖片的功能。 在頁面中的程式碼如下: <a onclick="show(show_attach?filePath="+path+"&type="+type+")"

一個Java線上和下載的例子

     前一段是有一個線上預覽的一個需求,本來打算用swf外掛,但是由於公司不讓用office產品,只能使用其他辦法來實現線上預覽,我整理了一個小例子,在這裡跟大家分享一下。          第一次寫微博有的地方表達的不清楚,請大家見諒哈 (以下是程式碼)注:前提是瀏覽

前端Ajax實現TXT文件(相容UTF-8ANCII,Unicode)

最近在做文件的預覽問題,在做txt的時候,原本是用HTML <object> 標籤,但是讀取的時候亂碼,經查詢資料,知道了原來txt文件預設的是ANCII編碼,解決辦法如下; 一、將ANCII轉Unicode,但是沒有成功, //ASCII 轉換 Unicod

java通過url線上Wordexcelpptpdftxt文件中的內容【只獲得其中的文字】

在頁面上顯示各種文件中的內容。在servlet中的邏輯 word:  BufferedInputStream bis = null;   URL url = null;   HttpURLConnection httpUrl = null; // 建立連結   url

ASP.NET MVC線上ExcelWordTXTPDF檔案

 話不多說直接上程式碼: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft

txtdocxlspptpdf檔案線上

txt、doc、xls、ppt、pdf檔案預覽   實現思路: android通過第三方API對映附件網路地址對附件進行線上預覽。 ios通過iframe對附件設定src進行線上預覽。 document.addEventListener( "plusready", fu

前端實現線上pdfwordxlsppt等檔案

1、前端實現pdf檔案線上預覽功能 方式一: 通過a標籤href屬性實現 pdf檔案理論上可以在瀏覽器直接開啟預覽但是需要開啟新頁面。在僅僅是預覽pdf檔案且UI要求不高的情況下可以直接通過a標籤href屬性實現預覽 <a href="文件地址"></

java實現pdf上傳下載線上刪除修改等功能

最近閒來無事,做了一個pdf的小功能,以供各位大神參考。下面是效果展示圖:功能主頁:點選上傳PDF按鈕:上傳檔案之後:線上預覽:開發環境:jdk_1.8                mysql_5.7開發工具:Eclipse                 navicat技

Asp.net MVC 利用(aspose+pdfobject.js) 實現線上wordexcelpptpdf檔案

線上預覽word、excel、ppt利用aspose動態生成html 主要程式碼 private bool OfficeDocumentToHtml(string sourceDoc, string saveDoc) { bool result = false;

java對officepdf文件線上解析(融合進專案中)

最近在專案中要做一個文件的預覽,在網上搜了好多demo,都可以實現其功能,但是放在自己的專案中有點複雜。 先說明本人的開發環境(win7+tomcat7+maven+svn+myeclipse),接下來最直觀的專案需求,如下圖: 當用戶點選預覽的時候能看各種文件,其實在這

office(如:WordExcelPPT 等)檔案輕鬆實現線上

解決方案有很多,比如可以先將檔案轉圖片或者pdf然後再網頁中顯示, 我在這裡說的可能並不適合大家,這裡簡單說下幾個快捷的方式 方案一: 可以直接使用第三方服務,不過這個需要收費的,我在這列幾個 http://www.yozodcs.com/ htt

java對文件等附件線上的詳細開發步驟解析:openOffice+SwfTools+FlexPaper

線上預覽實現簡介:     過程分為:1、使用OpenOffice(JODConverter)將不同型別的附件轉換為.pdf字尾的檔案(PDF);                       2、使用SwfTools將pdf檔案轉換為swf檔案;              

C# web實現word 轉Htmloffice轉Htmlpdf轉圖片 線上檔案

 改篇 pdf 預覽再本機沒問題,釋出再iis中 不行 ,(使用剪貼簿的問題..excel和word 可以,) 詳細配置及程式碼 word 轉Html 複製程式碼  1 /// <summary>  2         /// word轉成html  3  

jsp實現線上pdfwordxlsppt等檔案

最近在做一個共享數字化平臺,一些所涉功能知識記錄一下。 、其他教程寫得太老了,可能到如今已經不再適用。 1、jsp實現pdf檔案線上預覽功能 方式一、pdf檔案理論上可以在瀏覽器直接開啟預覽但是需要開啟新頁面。在僅僅是預覽pdf檔案且UI要求不高的情況下可以直

h5線上doxxlspptpdf

h5線上預覽dox、xls、ppt、pdf        App中上傳附件的伺服器後,需要對附件進行預覽; webApp可以通過兩種方式實現, 1、將附件下載到本地,通過plus.runtime.openFile開啟本地檔案 2、