Java Tip 94: How to open a non-HTML format with servlet
Simple way to send non-HTML format documents to the 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 non-HTML format documents, including Adobe PDF, Microsoft Word, and MICORSOFT EXCEL. In fact, these non-HTML formats can be sent using a servlet as long as they can be represented by MIME type. This article will use the PDF and Microsoft Word files as an example to show you how to transfer non-HTML format files using servlets, and how to interact with firewalls.
You can use servlet to open a file in your browser using servlets in the output stream of the Servlet. Although this looks very simple, pay attention to some points when opening non-HTML format documents (such as binary data or multimedia files).
First start from obtaining the output stream of the servlet:
ServletOutputStream out = res. maxOutputStream ();
Use MIME (MultipurPos Internet Mail Extension Multi-purpose Internet Mail Extension Protocol) to transfer mixed formats, multimedia and binary data files. If you want to open a document in the Servlet's Response object, you must set the MIME type of the document. In this example we will open the PDF document.
MIME Type Web Browser uses MIME types to identify non-HTML documents and determine how data within this document. Use the plug-in to use the MIME type, and when the web browser downloads the document indicated by the MIME type, you can start the corresponding plugin to process this document. Some MIME types can also be used in conjunction with external programs, and the corresponding external program will be launched after the browser downloads the document.
MIME type is very useful. They allow web browsers to handle documents in different formats, but do not need to embed relevant knowledge in advance. Java Servlets can use the MIME type to deliver non-HTML files to your browser, such as Adobe PDF and MICORSOFT WORD. Using the correct MIME type ensures that these non-HTML files are displayed correctly or external programs. The data section of this article provides some URLs to some articles that have defined the MIME type list and about MIME types.
The MIME type of the PDF file is "Application / PDF". To open a PDF document with a servlet, you need to set the content type of the HEADER in the Response object to "Application / PDF":
// mime type for pdf docres.setContentType ("Application / PDF");
To open a Microsoft Word document, you will set the content type of the Response object to "Application / Msword":
// mime type for msword docres.setContentType ("Application / Msword");
If it is an Excel document, use the MIME type "Application / VND.ms-Excel". Where VND indicates the manufacturer of the application, it must be included in the MIME type to open this type of document.
Sometimes the browser cannot identify the MIME type of the document. This is usually caused by a plug-in that these documents needed to be installed. In this case, the browser will pop up a dialog box, ask if the user needs to open the file or save it onto the local disk. Content Disposition A HTTP Response Header called Content-Disposition allows servlet to specify information indicated by documentation. With this header, you can specify a document separately (instead of opening in the browser), and can also be displayed according to the user's operation. If the user wants to save the document, you can also recommend a file name for the document. This suggestion name will appear in the File Name column of the Save AS dialog. If not specified, the name of the servlet will appear in the dialog. For more information on Content-Disposition Header, you can refer to the information.
In servlet, you need to set the header to the following:
Res.SetHeader ("Content-Disposition", "Attachment; FileName =" "EXAMPLE.PDF"); // attachment - Since We don't want to open // it in the browser, but // with adobe Acrobat, And set the // default file name to us.
If you want to open a Microsoft Word file, you can set it:
Res.SetHeader ("Content-Disposition", "Attachment; FileName" "EXAMPLE.DOC");
After the package is not HTML document, it is very simple to complete the above work. You need to create a java.net.URL object based on the name of the file to be sent. The string to the URL constructor must be a valid URL address pointing to the file. In this example, I want to open the documentation in Adobe Employment format:
String fileurl = "http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.pdf;"
Your URL string can also be similar to http://www.gr.com/pub/somefile.doc or http://www.gr.com/pub/somefile.xls. However, it is important to ensure that the file type to be transferred is consistent with the MIME type previously set in the HTTP Response object.
URL URL = New URL (fileurl);
Firewall If you need to pass the firewall, the last thing to consider is your URL link. First, you should collect information about the proxy server used, such as host names and port numbers, etc. For more information on how to establish links through the firewall, you can refer to the following information section.
If you are using Java 2, you should create a URLConnection object from the URL object class and set the following system properties:
URLConnection conn = url.openConnection (); // Use the username and password you use to // connect to the outside world // if your proxy server requires authentication.String authentication = "Basic" newsun.misc.BASE64Encoder (). Encode ("UserName: Password"; system.getproperties (). put ("proxyset", "true"); system.getproperties (). put ("proxyhost", proxy_host; // Your proxy hostsystem .GetProperties (). Put ("proxyport", proxy_port); // Your proxy portconn.seuestProperty ("proxy-authorization"; if you are using JDK 1.1, you cannot set these system properties. In this case, you can create a java.net.URL object according to the information used by the proxy server:
URL = New URL ("http", proxy_host, integer.parseint (proxy_port), fileurl; // Assumes Authentication is not Required
In-depth work began to read the input stream InputStream from the URLConnection (or URL) object before reading the document you transmit. In this example, the InputStream is encapsulated with BufferedInputStream.
If you use urlConnection, you can try the following code:
BufferedInputStream Bis = NewBufferedInputStream ());
If you use the URL, you can use the following code:
BufferedInputStream Bis = NewBufferedInputStream (Url.OpenStream ());
Once you do the above, just simply use the byte in the InputStream to the output stream of the servlet OutputStream:
BufferedOutputStream bos = newBufferedOutputStream (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);
In the final block, close these streams.
This example is implemented using dopost (Dopost is a method of httpservlet subclass):
Public void dopost (httpservletRequest Req, httpservletResponse res) throws servletexception, ioException {servletoutputstream out = res. maxOutputStream (); // ------------------------ --------------------------------------- // 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 SHOULD 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 tentesread
// 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; Final IOException E) {system.out.println ("IOException."); Throw e;} finally {ix (bis! = Null) Bis.Close (); if (bos! = null) bos.close ();}} Conclusion As you read, use servlet to open non-HTML documents fairly simple. This is true even if it is to pass the firewall. As long as the correct MIME type is set, you can use the same code to open the image or other multimedia files. Today's internet contains a lot of information, many of which are stored as non-HTML formats. Using servlets overcome HTML restrictions, simply easily deliver information about these non-HTML formats to users.
A consultant from the author Marla Bonar, Arizona Greenbrier & Russel in Phoenix, has been engaged in Java programming since JDK 1.0.2 has emerged. She is a faithful support for object-oriented architecture and design and software model. Under her father's encouragement, becomes a software engineer.
data
More information about MIME can be found in RFC: 2045, 2046, 2047, 822. To view these RFCs, you can visit: http://www.rfc-editor.org/rfcsearch.html More information about Content-Disposition Headers, see RFC 2183: RFC2183.txt
If you create a link through the firewall, more detailed information can be found in the following java points:
"Java TIP 42: Writing Java apps based on the firewall working on the proxy server," Ron Kurr (javaworld): http://www.javaworld.com/javaworld/javatips/jw-javatip42.html "Java Tip 46: Used Java 1.2's Authenticator class, "John Zukowski (JavaWorld): http://www.javaworld.com/javaworld/javatips/jw-javatip46.html" Java Tip 47: URL Reconfirm, "John Zukowski (JavaWorld): http : //www.ibm.com/developerWorks/java/jw-tips/tip047/index.shtml