• 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 URL(urlReal);
      9 httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
     10 httpUrl.connect();// 获取网络输入流
     11 bis = new BufferedInputStream(httpUrl.getInputStream());
     12  
     13 String bodyText = null;
     14 WordExtractor ex = new WordExtractor(bis);
     15 bodyText = ex.getText();
     16 response.getWriter().write(bodyText);
     17  
     18 excel:
     19  
     20 BufferedInputStream bis = null;
     21 URL url = null;
     22 HttpURLConnection httpUrl = null; // 建立链接
     23 url = new URL(urlReal);
     24 httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
     25 httpUrl.connect();// 获取网络输入流
     26 bis = new BufferedInputStream(httpUrl.getInputStream());
     27  
     28 content = new StringBuffer();
     29 HSSFWorkbook workbook = new HSSFWorkbook(bis);
     30 for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
     31 HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个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.getRow(rowNum);
     39 if (null == aRow) {
     40 continue;
     41 }
     42 for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {
     43 HSSFCell aCell = aRow.getCell(cellNum);
     44 if (null == aCell) {
     45 continue;
     46 }
     47 if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
     48 content.append(aCell.getRichStringCellValue()
     49 .getString());
     50 } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
     51 boolean b = HSSFDateUtil.isCellDateFormatted(aCell);
     52 if (b) {
     53 Date date = aCell.getDateCellValue();
     54 SimpleDateFormat df = new SimpleDateFormat(
     55 "yyyy-MM-dd");
     56 content.append(df.format(date));
     57 }
     58 }
     59 }
     60 }
     61 }
     62 response.getWriter().write(content.toString());
     63  
     64 ppt:
     65  
     66 BufferedInputStream bis = null;
     67 URL url = null;
     68 HttpURLConnection httpUrl = null; // 建立链接
     69 url = new URL(urlReal);
     70 httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
     71 httpUrl.connect();// 获取网络输入流
     72 bis = new BufferedInputStream(httpUrl.getInputStream());
     73  
     74 StringBuffer content = new StringBuffer("");
     75 SlideShow ss = new SlideShow(new HSLFSlideShow(bis));
     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.append(t[j].getText());
     81 }
     82 content.append(slides[i].getTitle());
     83 }
     84 response.getWriter().write(content.toString());
     85  
     86 pdf:
     87  
     88 BufferedInputStream bis = null;
     89 URL url = null;
     90 HttpURLConnection httpUrl = null; // 建立链接
     91 url = new URL(urlReal);
     92 httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
     93 httpUrl.connect();// 获取网络输入流
     94 bis = new BufferedInputStream(httpUrl.getInputStream());
     95  
     96 PDDocument pdfdocument = null;
     97 PDFParser parser = new PDFParser(bis);
     98 parser.parse();
     99 pdfdocument = parser.getPDDocument();
    100 ByteArrayOutputStream out = new ByteArrayOutputStream();
    101 OutputStreamWriter writer = new OutputStreamWriter(out);
    102 PDFTextStripper stripper = new PDFTextStripper();
    103 stripper.writeText(pdfdocument.getDocument(), writer);
    104 writer.close();
    105 byte[] contents = out.toByteArray();
    106  
    107 String ts = new String(contents);
    108 response.getWriter().write(ts);
    109  
    110 txt:
    111  
    112 BufferedReader bis = null;
    113 URL url = null;
    114 HttpURLConnection httpUrl = null; // 建立链接
    115 url = new URL(urlReal);
    116 httpUrl = (HttpURLConnection) url.openConnection();// 连接指定的资源
    117 httpUrl.connect();// 获取网络输入流
    118 bis = new BufferedReader( new InputStreamReader(httpUrl.getInputStream()));
    119  
    120 StringBuffer buf=new StringBuffer();
    121 String temp;
    122 while ((temp = bis.readLine()) != null) {
    123 buf.append(temp);
    124 response.getWriter().write(temp);
    125 if(buf.length()>=1000){
    126 break;
    127 }
    128 }
    129 bis.close();
  • 相关阅读:
    文件操作
    内置函数
    lambda表达式
    函数基础
    基础数据类型-dict
    基础数据类型-tuple
    基础数据类型-list
    基础数据类型-set
    Python开发【第三篇】基本数据类型
    Python开发【第二篇】运算符
  • 原文地址:https://www.cnblogs.com/lr393993507/p/5542901.html
Copyright © 2020-2023  润新知