How to open a document in a non-HTML format with a servlet (turn)

Source: Internet
Author: User
Tags format final header integer rfc client port number firewall
Servlet How to open a document in a non-HTML format with a servlet
A simple way to send a non-HTML document to a WEB client

by Marla Bonar (marla.bonar@javaworld.com)

Summary
Java Servlet Programming makes it easy to send HTML files to a client Web browser. However, many sites also allow access to documents that are not in HTML format, including Adobe PDFs, Microsoft Word and Micorsoft Excel, and so on. In fact, these non-HTML formats can be sent using the servlet as long as they can be represented by MIME types. This article takes a PDF and Microsoft Word files as an example to show you how to use the servlet to transfer non-HTML format files and how to interact with the firewall. [I] [I]
You can use the servlet to open a file in the browser as long as you write the file to the output stream in the servlet. Although this may seem very simple, there are some important points to keep in mind when you open a document in a non-HTML format, such as binary data or multimedia files.
Start by getting the output stream from the servlet:
Servletoutputstream out = Res.getoutputstream ();

Use MIME (multipurpos Internet Mail Extension Multicast protocol) on the Internet to transfer mixed format, multimedia, and binary data files. If you want to open a document in the response object of the servlet, you must set the MIME type of the document. In the following example, we will open the PDF document.
MIME Type
Web browsers use MIME types to identify non-HTML documents and decide how to display the data within that document. Using a plug-in (plug-in) in conjunction with a MIME type, the Web browser can start the appropriate plug-in to process the document when it downloads the document indicated by the MIME type. Some MIME types can also be used in conjunction with external programs, and the browser will start the appropriate external program after downloading the document.
MIME types are useful. They allow WEB browsers to process documents in different formats without having to embed relevant knowledge beforehand. Java Servlets can use MIME types to transfer non-HTML files to browsers, such as Adobe PDFs and Micorsoft Word. Use the correct MIME type to ensure that these non-HTML files are displayed by the correct plug-in or external program. The information section at the end of this article provides URLs that point to a list of defined MIME types and articles 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 doc
Res.setcontenttype ("Application/pdf");

To open a Microsoft Word document, you set the content type of the response object to "Application/msword":
MIME type for MSWord doc
Res.setcontenttype ("Application/msword");

If it is an Excel document, the MIME type "application/vnd.ms-excel" is used. Where VND represents the creator of the application, it must be included in the MIME type to open the type of document.
Sometimes the browser does not recognize the MIME type of the document. This is usually caused by a plugin that is not required for these documents to be installed. In this case, the browser pops up a dialog box asking if the user needs 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 that the document represents. With this header, you can specify that the document be opened individually (not in a browser), and can be displayed according to the user's actions. If the user wants to save the document, you can also suggest a file name for the document. This suggestion name appears in the file Name column of the Save as dialog box. If not specified, the name of the servlet appears in the dialog box. For more information on the content-disposition header, refer to the information.
In the 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 use.

If you want to open a Microsoft Word file, you can set it to:
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 create a Java.net.URL object based on the name of the file you want to transfer. The string assigned to the URL constructor must be a valid URL address to the file. In this example, I want to open a document in Adobe employment format:
String FileURL =
"Http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.pdf;"

Your URL string can also resemble Http://www.gr.com/pub/somefile.doc or Http://www.gr.com/pub/somefile.xls. However, you must ensure that the file type to be routed is the same as the MIME type previously set in the HTTP response object.
URL url = new URL (fileURL);

Firewalls
If you need to go through a firewall, the last thing to consider is your URL link. First, you should collect information about the proxy server used, such as host name and port number. For more information on how to create a link through a firewall, see the data section below.
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 for you
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 properties. In this case, you can create the Java.net.URL object based on the information of the proxy server you are using:
url = new URL ("http", Proxy_host,
Integer.parseint (Proxy_port),
FileURL);
Assumes authentication is not required

work in Depth
Before you start reading your routed document, first get the input stream InputStream from the URLConnection (or URL) object. In this example, the InputStream is encapsulated with Bufferedinputstream.
If you use URLConnection, you can try the following code:
Bufferedinputstream bis = new
Bufferedinputstream (Conn.getinputstream ());

If you use a URL, the following code is available:
Bufferedinputstream bis = new
Bufferedinputstream (Url.openstream ());

Once you have done so, simply write the bytes in the InputStream to the servlet's output stream OutputStream:
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);
}

In the last block of code, close these streams.
This example is implemented using DoPost (DoPost is a method of the HttpServlet subclass):
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 ...
//------------------------------------------------------------
Res.setheader ("Content-disposition",
"Attachment; Filename= "+ +
"Example.pdf");

//-----------------------------------------------------------------
Proxy_host and Proxy_port should be your PROXY HOST and PORT
That'll let's 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 the 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 ();
}
}

Conclusions
As you can read, opening a non-HTML document with a servlet is fairly straightforward. This is true even if you are going through a firewall. You can use the same code to open a picture or other multimedia file as long as the correct MIME type is set. Today's Internet contains a large amount of information, many of which are stored in non-HTML format. Using the servlet to overcome HTML restrictions, it is easy and easy to transfer these non-HTML information to the user.
about the author
Marla Bonar (marla.bonar@javaworld.com), a consultant Arizona, State Greenbrier & Russel in Phoenix, has been working in Java programming since the advent of JDK 1.0.2. She is a loyal supporter of object-oriented architecture and design and software patterns. With the encouragement of her father, she became a software engineer.
Information
    • More information about MIME can be found in rfc:2045,2046,2047,822. To view these RFCs, you can access:
      Http://www.rfc-editor.org/rfcsearch.html
    • For more information on content-disposition headers, see RFC 2183:
      Rfc2183.txt
If you are building a link through a firewall, see the following Java essentials for more detailed information:
    • "Java TIP 42: Write Java applications that work with a firewall based on a proxy server," Ron kurr (javaworld):
      Http://www.javaworld.com/javaworld/javatips/jw-javatip42.html
    • "Java TIP 46: Use Java 1.2 's authenticator class," John Zukowski (javaworld):
      Http://www.javaworld.com/javaworld/javatips/jw-javatip46.html
    • "47:url of Java Tip," John Zukowski (javaworld):
      Http://www.ibm.com/developerWorks/cn/java/jw-tips/tip047/index.shtml


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.