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

  1 在页面上显示各种文档中的内容。在servlet中的逻辑
  2  
  3 word:
  4  
  5 BufferedInputStream bis = null;
  6 URL url = null;
  7 HttpURLConnection httpUrl = null; // 建立链接
  8 url = new URLurlReal);
  9 httpUrl = HttpURLConnection) url.openConnection);// 连接指定的资源
 10 httpUrl.connect);// 获取网络输入流
 11 bis = new BufferedInputStreamhttpUrl.getInputStream));
 12  
 13 String bodyText = null;
 14 WordExtractor ex = new WordExtractorbis);
 15 bodyText = ex.getText);
 16 response.getWriter).writebodyText);
 17  
 18 excel:
 19  
 20 BufferedInputStream bis = null;
 21 URL url = null;
 22 HttpURLConnection httpUrl = null; // 建立链接
 23 url = new URLurlReal);
 24 httpUrl = HttpURLConnection) url.openConnection);// 连接指定的资源
 25 httpUrl.connect);// 获取网络输入流
 26 bis = new BufferedInputStreamhttpUrl.getInputStream));
 27  
 28 content = new StringBuffer);
 29 HSSFWorkbook workbook = new HSSFWorkbookbis);
 30 for int numSheets = 0; numSheets < workbook.getNumberOfSheets); numSheets++) {
 31 HSSFSheet aSheet = workbook.getSheetAtnumSheets);// 获得一个sheet
 32 content.append"/n");
 33 if null == aSheet) {
 34 continue;
 35 }
 36 for int rowNum = 0; rowNum <= aSheet.getLastRowNum); rowNum++) {
 37 content.append"/n");
 38 HSSFRow aRow = aSheet.getRowrowNum);
 39 if null == aRow) {
 40 continue;
 41 }
 42 for short cellNum = 0; cellNum <= aRow.getLastCellNum); cellNum++) {
 43 HSSFCell aCell = aRow.getCellcellNum);
 44 if null == aCell) {
 45 continue;
 46 }
 47 if aCell.getCellType) == HSSFCell.CELL_TYPE_STRING) {
 48 content.appendaCell.getRichStringCellValue)
 49 .getString));
 50 } else if aCell.getCellType) == HSSFCell.CELL_TYPE_NUMERIC) {
 51 boolean b = HSSFDateUtil.isCellDateFormattedaCell);
 52 if b) {
 53 Date date = aCell.getDateCellValue);
 54 SimpleDateFormat df = new SimpleDateFormat
 55 "yyyy-MM-dd");
 56 content.appenddf.formatdate));
 57 }
 58 }
 59 }
 60 }
 61 }
 62 response.getWriter).writecontent.toString));
 63  
 64 ppt:
 65  
 66 BufferedInputStream bis = null;
 67 URL url = null;
 68 HttpURLConnection httpUrl = null; // 建立链接
 69 url = new URLurlReal);
 70 httpUrl = HttpURLConnection) url.openConnection);// 连接指定的资源
 71 httpUrl.connect);// 获取网络输入流
 72 bis = new BufferedInputStreamhttpUrl.getInputStream));
 73  
 74 StringBuffer content = new StringBuffer"");
 75 SlideShow ss = new SlideShownew HSLFSlideShowbis));
 76 Slide[] slides = ss.getSlides);
 77 for int i = 0; i < slides.length; i++) {
 78 TextRun[] t = slides[i].getTextRuns);
 79 for int j = 0; j < t.length; j++) {
 80 content.appendt[j].getText));
 81 }
 82 content.appendslides[i].getTitle));
 83 }
 84 response.getWriter).writecontent.toString));
 85  
 86 pdf:
 87  
 88 BufferedInputStream bis = null;
 89 URL url = null;
 90 HttpURLConnection httpUrl = null; // 建立链接
 91 url = new URLurlReal);
 92 httpUrl = HttpURLConnection) url.openConnection);// 连接指定的资源
 93 httpUrl.connect);// 获取网络输入流
 94 bis = new BufferedInputStreamhttpUrl.getInputStream));
 95  
 96 PDDocument pdfdocument = null;
 97 PDFParser parser = new PDFParserbis);
 98 parser.parse);
 99 pdfdocument = parser.getPDDocument);
100 ByteArrayOutputStream out = new ByteArrayOutputStream);
101 OutputStreamWriter writer = new OutputStreamWriterout);
102 PDFTextStripper stripper = new PDFTextStripper);
103 stripper.writeTextpdfdocument.getDocument), writer);
104 writer.close);
105 byte[] contents = out.toByteArray);
106  
107 String ts = new Stringcontents);
108 response.getWriter).writets);
109  
110 txt:
111  
112 BufferedReader bis = null;
113 URL url = null;
114 HttpURLConnection httpUrl = null; // 建立链接
115 url = new URLurlReal);
116 httpUrl = HttpURLConnection) url.openConnection);// 连接指定的资源
117 httpUrl.connect);// 获取网络输入流
118 bis = new BufferedReader new InputStreamReaderhttpUrl.getInputStream)));
119  
120 StringBuffer buf=new StringBuffer);
121 String temp;
122 while temp = bis.readLine)) != null) {
123 buf.appendtemp);
124 response.getWriter).writetemp);
125 ifbuf.length)>=1000){
126 break;
127 }
128 }
129 bis.close);

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注