Java tip 94: How to Use servlet to open documents in non-HTML Format

Source: Internet
Author: User
Tags rfc


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". WhereVNDIndicates 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. urlObject. 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.docOrHttp://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 createUrlconnectionAnd 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. urlObject:

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 exampleBufferedinputstreamSetInputstreamEncapsulation.

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 needInputstreamThe bytes written to the servlet output stream.OutputstreamMedium:

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(DopostYesHttpservletSubclass 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
Reprinted from javaworld magazine with permission. Web Publishing Inc. (An IDG Communication Company) is copyrighted. Register to get the edited email notice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.