Java tip 94: How to Use servlet to open documents in non-HTML Format
A Simple Method for sending non-HTML documents to a Web Client
By Marla Bonar
Summary
Java Servlet programming can easily send HTML files to the client web browser. However, many sites also allow access to documents in non-HTML format, including Adobe PDF, Microsoft Word, and micorsoft excel. In fact, as long as these non-HTML formats can be expressed as MIME types, servlet can be used for sending. This document uses PDF and Microsoft Word files as examples to show you how to use servlet to send non-HTML files and how to interact with the firewall.
You only need to write the file to the servlet output stream, you can use the servlet to open a file in the browser. Although this looks simple, you should pay attention to some important points when opening non-HTML documents (such as binary data or multimedia files. First, start from obtaining the servlet output stream: Servletoutputstream out = res. getoutputstream ();
Mime (multipurpos Internet Mail Extension multi-destination Internet Mail Extension Protocol) is used on the Internet to transmit mixed formats, multimedia and binary data files. To open a document in the servlet response object, you must set the MIME type of the document. In the following example, we will open the PDF document. Mime Type
The Web browser uses the MIME type to identify non-HTML documents and determines how to display the data in the documents. When the plug-in (plug-in) is used in combination with the MIME type, the corresponding plug-in can be started to process the document when the web browser downloads the MIME type indication document. Some MIME types can also be externalProgramIn combination, the browser starts the corresponding external program after downloading the document. MIME type is very useful. They allow Web browsers to process documents in different formats, but do not need to embed relevant knowledge in advance. Java Servlets can use the MIME type to send non-HTML files to a browser, such as Adobe PDF and micorsoft word. The correct MIME type ensures that these non-HTML files are displayed by the correct plug-in or external programs. The documents at the end of this article provide some URLs pointing to some lists of MIME types that have been defined andArticle. The MIME type of the PDF file is"Application/pdf" . To open a PDF file using Servlet, set the content type of the header in the response object"Application/pdf" : // MIME type for PDF Doc
Res. setcontenttype ("application/pdf ");
To open a Microsoft Word document, set the content type of the response object"Application/MSWord" : // MIME type for MSWord Doc
Res. setcontenttype ("application/MSWord ");
For an Excel file, the MIME type is used."Application/vnd. MS-excel" . WhereVND Indicates the creator of the application, which must be included in the MIME type to open this type of document. Sometimes the browser cannot recognize the MIME type of the document. This is usually because the plug-ins required by these documents are not installed. In this case, the browser will pop up a dialog box asking whether you need to open the file or save it to a local disk. Content Disposition
An HTTP Response Header called content-Disposition allows the servlet to specify the information represented in the document. With this header, you can specify the document to be opened separately (instead of in the browser) and display it according to user operations. If you want to save a document, you can also recommend a file name for this document. The recommended name will appear in the "file name" column of the Save As dialog box. If not specified, the servlet name will appear in the dialog box. For more information about the content-Disposition Header, see documents. In servlet, you need to set the header as follows: Res. setheader ("content-disposition ",
"Attachment; filename =" +
"Example.pdf ");
// Attachment-since we don't want to open
// It in the browser,
// With Adobe Acrobat, and set
// Default file name to use.
If you want to open a Microsoft Word file, you can set it: Res. setheader ("content-disposition ",
"Attachment; FILENAME" +
"Example.doc ");
Encapsulate non-HTML documents
After completing the above work, the rest is very simple. You need to createJava.net. url Object. The string sent to the URL constructor must be a valid URL address pointing to the file. In this example, I want to open a document in Adobe employee format: String fileurl =
"Http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.htm ;"
Your URL string can also be similarHttp://www.gr.com/pub/somefile.doc OrHttp://www.gr.com/pub/somefile.xls . However, make sure that the type of the file to be transferred is consistent with the MIME type set in the HTTP Response object. URL url = new URL (fileurl );
Firewall
If you need to use the firewall, the last thing to consider is your URL link. First, collect information about the proxy server used, such as the host name and port number. For more information about how to establish a link through the firewall, see the following documents. If Java 2 is used, you should createUrlconnection And set the following system properties: Urlconnection conn = URL. openconnection (); // Use the username and password you use
// Connect to the outside world
// If your proxy server requires authentication.
String authentication = "Basic" + new
Sun. Misc. base64encoder (). encode ("username: Password". getbytes ()); System. getproperties (). Put ("proxyset", "true "); System. getproperties (). Put ("proxyhost", proxy_host); // your proxy host
System. getproperties (). Put ("proxyport", proxy_port); // your proxy Port
Conn. setrequestproperty ("proxy-Authorization", authentication );
If you are using JDK 1.1, you cannot set these system attributes. In this case, you can createJava.net. url Object: Url = new URL ("HTTP", proxy_host,
Integer. parseint (proxy_port ),
Fileurl );
// Assumes authentication is not required
In-depth work
Before reading your documentsUrlconnection (OrURL ) Object to obtain the input streamInputstream . In this exampleBufferedinputstream SetInputstream Encapsulation. If you useUrlconnection , You can try the followingCode: Bufferedinputstream Bis = new
Bufferedinputstream (conn. getinputstream ());
If you useURL , The following code is available: Bufferedinputstream Bis = new
Bufferedinputstream (URL. openstream ());
Once you complete the preceding operations, you simply needInputstream The bytes written to the servlet output stream.Outputstream Medium: Bufferedoutputstream Bos = new
Bufferedoutputstream (out ); Byte [] buff = new byte [2048];
Int bytesread; // Simple read/write loop.
While (-1! = (Bytesread = bis. Read (buff, 0, Buff. Length ))){
Bos. Write (buff, 0, bytesread );
}
Close these streams in the final code block. This example usesDopost (Dopost YesHttpservlet Subclass method ): Public void dopost (httpservletrequest req,
Httpservletresponse res)
Throws servletexception, ioexception
{
Servletoutputstream out =
Res. getoutputstream (); //---------------------------------------------------------------
// Set the output data's MIME type
//--------------------------------------------------------------- Res. setcontenttype ("application/pdf"); // MIME type for PDF Doc //---------------------------------------------------------------
// Create an input stream from fileurl
//--------------------------------------------------------------- String fileurl =
"Http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.pdf "; //------------------------------------------------------------
// Content-Disposition Header-don't open in browser and
// Set the "Save as..." filename.
// * There is reportedly a bug in ie4.0 which ignores this...
//------------------------------------------------------------
Res. setheader ("content-disposition ",
"Attachment; filename =" + =
"Example.pdf "); //-----------------------------------------------------------------
// Proxy_host and proxy_port shoshould be your proxy host and Port
// That will let you go through the firewall without authentication.
// Otherwise set the system properties and use urlconnection. getinputstream ().
//-----------------------------------------------------------------
Bufferedinputstream Bis = NULL;
Bufferedoutputstream Bos = NULL; Try {
URL url = new URL ("HTTP", proxy_host,
Integer. parseint (proxy_port), fileurl ); // Use buffered stream for reading/writing.
Bis = new bufferedinputstream (URL. openstream ());
Bos = new bufferedoutputstream (out ); Byte [] buff = new byte [2048];
Int bytesread; // Simple read/write loop.
While (-1! = (Bytesread = bis. Read (buff, 0, Buff. Length ))){
Bos. Write (buff, 0, bytesread );
} } Catch (final malformedurlexception e ){
System. Out. println ("malformedurlexception .");
Throw E;
} Catch (final ioexception e ){
System. Out. println ("ioexception .");
Throw E;
} Finally {
If (Bis! = NULL)
Bis. Close ();
If (Bos! = NULL)
Bos. Close ();
}
}
Conclusion
As you have read, using Servlet to open non-HTML documents is quite simple. This is true even if you want to use a firewall. If the MIME type is set correctly, you can use the same code to open images or other multimedia files. Today's Internet contains a large amount of information, many of which are stored in non-HTML format. Servlet can be used to overcome HTML restrictions and easily transmit non-HTML information to users.
About the author
Marla Bonar, a consultant at Greenbrier & Russel in Phoenix in Arizona, has been engaged in Java programming since JDK 1.0.2. She is a loyal supporter of object-oriented architecture, design, and software models. She was encouraged by her father to become a software engineer. |
Materials
- For more information about mime, see RFC 2047,822. To view these RFC, you can access:
Http://www.rfc-editor.org/rfcsearch.html
- For more information about the content-Disposition Header, see RFC 2183:
Rfc2183.txt
For more detailed information, see the following Java points:
- "Java tip 42: Write a Java application using a firewall working on a proxy server," Ron Kurr(Javaworld ):
Http://www.javaworld.com/javaworld/javatips/jw-javatip42.html
- "Java tip 46: Use the Java 1.2 authenticator class," John zukoski(Javaworld ):
Http://www.javaworld.com/javaworld/javatips/jw-javatip46.html
- "Java tip 47: reconfirm the URL," John zukoski(Javaworld ):
Http://www.ibm.com/developerWorks/java/jw-tips/tip047/index.shtml
|