博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java在线预览txt、word、ppt、execel,pdf代码
阅读量:7091 次
发布时间:2019-06-28

本文共 3594 字,大约阅读时间需要 11 分钟。

hot3.png

 

在页面上显示各种文档中的内容。在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();

转载于:https://my.oschina.net/u/734885/blog/498444

你可能感兴趣的文章
多个站点的VSFTPD
查看>>
初级篇第一期:从0开始
查看>>
窗体上有一JTable,如何让数据库里的数据在表格里显示出来
查看>>
如何在A10的AX上实现TACACS+的AAA认证
查看>>
文件的元数据信息,时间戳,别名,练习
查看>>
蜕变:从菜鸟到Linux安全专家 图书简介、前言、目录发布
查看>>
VS2017 远程调试小记
查看>>
php文件上传接口及文件上传错误服务器配置
查看>>
[实战]MVC5+EF6+MySql企业网盘实战(5)——页面模板
查看>>
CSS 参考手册
查看>>
如何用c++发出音乐
查看>>
JLOI2015 城池攻占
查看>>
基本数据类型
查看>>
Can you solve this equation? 详细解答
查看>>
exit命令详解
查看>>
date命令详解
查看>>
C#序列化与反序列化
查看>>
首页logo的代码标志性写法,方便SEO
查看>>
Collection of Tools - Fix problems that programs cannot be installed or uninstalled
查看>>
DP+高精度 URAL 1036 Lucky Tickets
查看>>