How to open non-HTML format documents with servlet

zhaozj2021-02-08  383

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

The web browser uses the MIME type to identify non-HTML documents and determine how data within the 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 DOC

Res.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 DOC

Res.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 document. 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 USE.

If you want to open a Microsoft Word file, you can set it:

Res.SetHeader ("Content-Disposition",

"attachment; filename"

"EXample.doc");

Package non-HTML document

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 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" 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 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

Before you start reading the document you transfer, you 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 the URL, you can use the following code:

BufferedInputStream Bis = New

BufferedInputStream (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 = 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 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. GetOutputStream ();

/ / -------------------------------------------------------------------------------------------- ---------------

// set the output data's mime type

/ / -------------------------------------------------------------------------------------------- ---------------

Res.SetContentType ("Application / PDF"); // Mime Type for PDF DOC

/ / -------------------------------------------------------------------------------------------- --------------- // Create An Input Stream 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 shouth 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 forreading / write.

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 iexception e) {

System.out.println ("IOEXCEPTION.");

Throw e;

} finally {

IF (bis! = null) bis.close ();

IF (bos! = null)

Bos.close ();

}

}

in conclusion

As you read, use servlet to open non-HTML documents quite 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.

转载请注明原文地址:https://www.9cbs.com/read-893.html

New Post(0)