日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java在线预览txt、word、ppt、execel,pdf代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載出處:http://blog.csdn.net/csh624366188/article/details/6683757


在頁面上顯示各種文檔中的內容。在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();


總結

以上是生活随笔為你收集整理的java在线预览txt、word、ppt、execel,pdf代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。