Original address: Click on the Open link
Displays the contents of various documents on the page. Logic in the servlet
Word:
Bufferedinputstream bis = null;
URL url = null;
HttpURLConnection httpurl = null; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
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; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
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);//Get a 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; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
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; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
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; Create a link
url = new URL (urlreal);
Httpurl = (httpurlconnection) url.openconnection ();//Connection specified resource
Httpurl.connect ()//get network input stream
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 ();