在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

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

2016-12-20 13:15| 发布者: zhangjf| 查看: 489| 评论: 0

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

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 10:53

Copyright 2015-2025 djqfx

返回顶部