文章正文
java在线预览txt、word、ppt、execel,pdf代码
【文档】2020-04-23
简介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);
text:
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();
打赏支持
感谢您的支持,加油!
打开微信扫码打赏,你说多少就多少
找书费时,联系客服快速获取!
在线客服8:30-22:30,若离线请留言!
获取教程,请联系在线客服!
在线客服8:30-22:30,若离线请留言!
热门阅读
-
然而,很美 爵士乐之书 (英)杰夫·戴尔GeoffDyer著;孔亚雷译 杭州:浙江文艺出版社 PDF 7533937850 2013.pdf
然而,很美 爵士乐之书 (英)杰夫·戴尔GeoffDyer著;孔亚雷译 杭州:浙江文艺出版社 PDF 7533937850 2013.pdf ...
-
红字 (美)霍桑著;姚乃强译 南京:译林出版社 PDF 7805679193 2012.pdf
红字 (美)霍桑著;姚乃强译 南京:译林出版社 PDF 7805679193 2012.pdf ...
-
一刀能割出多少爱 卓左右著 北京:光明日报出版社 PDF 7802062020 2006.pdf
一刀能割出多少爱 卓左右著 北京:光明日报出版社 PDF 7802062020 2006.pdf ...
-
春秋来信 张枣著 北京:文化艺术出版社 PDF 7503917253 1998.pdf
春秋来信 张枣著 北京:文化艺术出版社 PDF 7503917253 1998.pdf ...
-
历史在女人面前拐弯 中国卷 影响中国历史的16位女性 王本刚主编 北京:金城出版社 PDF 7800848221 2006.pdf
历史在女人面前拐弯 中国卷 影响中国历史的16位女性 王本刚主编 北京:金城出版社 PDF 7800848221 2006.pdf ...
-
清朝第一帝-努尔哈赤 阎崇年著 北京:华文出版社 PDF 7507519600 2006.pdf
清朝第一帝-努尔哈赤 阎崇年著 北京:华文出版社 PDF 7507519600 2006.pdf ...
-
保护公众健康 美国食品药品百年监管历程 (美)菲利普·希尔茨(Philip J. Hilts)著;姚明威译 北京:中国水利水电出版社 PDF 7508432037 2006.pdf
保护公众健康 美国食品药品百年监管历程 (美)菲利普·希尔茨(Philip J. Hilts)著;姚明威译 北京:中国水利水电出版社 PDF 7508432037 2006.pdf ...
-
大毒枭自白 与罂粟花一起盛开或凋谢 胡玥著 开封:河南大学出版社 PDF 7810913476 2005.pdf
大毒枭自白 与罂粟花一起盛开或凋谢 胡玥著 开封:河南大学出版社 PDF 7810913476 2005.pdf ...
-
9a班的恐吓事件 (德)(S.沃尔夫)Stefan Wolf著;李晶译 北京:北京少年儿童出版社 PDF 7530106988 1999.pdf
9a班的恐吓事件 (德)(S.沃尔夫)Stefan Wolf著;李晶译 北京:北京少年儿童出版社 PDF 7530106988 1999.pdf ...
-
汉语现代音韵学 杨剑桥著 上海:复旦大学出版社 PDF 7309016254 出版时间:1996.pdf
汉语现代音韵学 杨剑桥著 上海:复旦大学出版社 PDF 7309016254 出版时间:1996.pdf ...