今天在CSDN博客中看到这篇文章,感觉很有用就ctrl+v了一下
在页面上显示各种文档中的内容。在servlet中的逻辑
word:
BufferedInputStream bis = null;
URL url = null;
HttpURLConnection httpUrl =null; // 建立链接
url = new URL(urlReal);
httpUrl = (HttpURLConnection)url.openConnection();// 连接指定的资源
httpUrl.connect();//获取网络输入流
bis = newBufferedInputStream(httpUrl.getInputStream());
String bodyText = null;
WordExtractor ex = newWordExtractor(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 = newBufferedInputStream(httpUrl.getInputStream());
content = new StringBuffer();
HSSFWorkbook workbook = newHSSFWorkbook(bis);
for (int numSheets = 0;numSheets < workbook.getNumberOfSheets(); numSheets++) {
HSSFSheetaSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
content.append("/n");
if (null ==aSheet) {
continue;
}
for (introwNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {
content.append("/n");
HSSFRowaRow = aSheet.getRow(rowNum);
if(null == aRow) {
continue;
}
for(short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++){
HSSFCellaCell = 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) {
booleanb = HSSFDateUtil.isCellDateFormatted(aCell);
if(b) {
Datedate = aCell.getDateCellValue();
SimpleDateFormatdf = 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 = newBufferedInputStream(httpUrl.getInputStream());
StringBuffer content = new StringBuffer("");
SlideShow ss = newSlideShow(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 = newBufferedInputStream(httpUrl.getInputStream());
PDDocument pdfdocument = null;
PDFParser parser = newPDFParser(bis);
parser.parse();
pdfdocument =parser.getPDDocument();
ByteArrayOutputStream out = newByteArrayOutputStream();
OutputStreamWriter writer = newOutputStreamWriter(out);
PDFTextStripper stripper = newPDFTextStripper();
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 = newBufferedReader( newInputStreamReader(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();