java 在线预览 txt、word、ppt、execel,pdf 代码
在页面上显示各种文档中的内容。在 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) {