天天看點

Servlet 準确的輸出檔案格式

首先從獲得 servlet 的輸出流開始:

    ServletOutputStream out = res.getOutputStream();

網際網路上使用 MIME (multipurpos Internet mail extension 多目的網際網路郵件擴充協定)來傳送混合格式、多媒體和二進制資料檔案。如果要在 servlet 的 response 對象中打開某個文檔,就必須設定該文檔的 MIME 類型。下面這個例子中我們将打開 PDF 文檔。

MIME 類型

Web 浏覽器使用 MIME 類型來識别非 HTML 文檔,并決定如何顯示該文檔内的資料。将插件 (plug-in) 與 MIME 類型結合使用,則當 Web 浏覽器下載下傳 MIME 類型訓示的文檔時,就能夠啟動相應插件處理此文檔。某些 MIME 類型還可以與外部程式結合使用,浏覽器下載下傳文檔後會啟動相應的外部程式。

MIME 類型非常有用。它們允許 Web 浏覽器處理不同格式的文檔,卻不需要事先嵌入相關知識。Java Servlets 可以使用 MIME 類型來向浏覽器傳送非 HTML 檔案,比如 Adobe PDF 和 Micorsoft Word。使用正确的 MIME 類型能夠保證這些非 HTML 檔案被正确的插件或外部程式顯示。本文末的資料部分提供了一些網址,指向一些已定義 MIME 類型清單和關于 MIME 類型的文章。

PDF 檔案的 MIME 類型是 "application/pdf"。要用 servlet 來打開一個 PDF 文檔,需要将 response 對象中 header 的 content 類型設定成 "application/pdf":

    // MIME type for pdf doc

    res.setContentType( "application/pdf" );

若要打開一個 Microsoft Word 文檔,你就要将 response 對象的 content 類型設定成 "application/msword":

    // MIME type for MSWord doc

    res.setContentType( "application/msword" );

如果是一個 Excel 文檔,則使用 MIME 類型 "application/vnd.ms-excel"。其中 vnd 表示該應用程式的制造者,必須将它包含在 MIME 類型裡才能夠打開該類型文檔。

有時候浏覽器不能識别文檔的 MIME 類型。通常這是由于沒有安裝這些文檔需要的插件而導緻的。這種情況下,浏覽器會彈出一個對話框,詢問使用者是否需要打開該檔案或是将它儲存到本地磁盤上。

Content disposition

一種叫做 content-disposition 的 HTTP response header 允許 servlet 指定文檔表示的資訊。使用這種 header ,你就可以将文檔指定成單獨打開(而不是在浏覽器中打開),還可以根據使用者的操作來顯示。如果使用者要儲存文檔,你還可以為該文檔建議一個檔案名。這個建議名稱會出現在 Save As 對話框的“檔案名”欄中。如果沒有指定,則對話框中就會出現 servlet 的名字。更多關于 content-disposition header 的資訊,可以參閱資料。

在 servlet 中,你需要将 header 設定成下面這樣:

    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.

如果你要打開的是 Microsoft Word 檔案,你可以設成:

    res.setHeader("Content-disposition",

                  "attachment; filename" +

                  "Example.doc" );

封裝非 HTML 文檔

完成上述工作後,剩下的就非常簡單了。你需要根據待傳送檔案的名字,建立一個 java.net.URL 對象。交給 URL 構造器的字元串必須是指向該檔案的一個有效 URL 位址。在這個例子中,我要打開 Adobe employment 格式的文檔:

    String fileURL =

"http://www.adobe.com/aboutadobe/careeropp/pdfs/adobeapp.pdf;"

你的 URL 字元串也可以類似于 http://www.gr.com/pub/somefile.doc或 http://www.gr.com/pub/somefile.xls。但必須確定待傳送檔案類型與先前在 HTTP response 對象中設定的 MIME 類型一緻。

    URL url = new URL ( fileURL );

防火牆

如果需要通過防火牆,最後一件要考慮的事情就是你的 URL 連結。首先應當搜集所用代理伺服器的相關資訊,例如主機名稱和端口号等。更多關于如何通過防火牆建立連結的資訊,可以參看下面的資料部分。

如果使用的是 Java 2,你應該從 URL 對象類中建立一個 URLConnection 對象,并設定下列系統屬性:

    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);

如果你使用的是 JDK 1.1,則不能設定這些系統屬性。這種情況下,你可以根據所用代理伺服器的資訊建立 java.net.URL 對象:

    url = new URL("http", PROXY_HOST,

                  Integer.parseInt(PROXY_PORT),

                  fileURL );

    // assumes authentication is not required

深入工作

開始閱讀你傳送的文檔之前,首先要從 URLConnection (或 URL) 對象中獲得輸入流 InputStream。在這個例子中,用 BufferedInputStream 将 InputStream 封裝起來。

如果你采用 URLConnection,可以嘗試如下代碼:

    BufferedInputStream bis = new

        BufferedInputStream(conn.getInputStream());

如果你使用 URL,則可用下列代碼:

    BufferedInputStream bis = new

        BufferedInputStream(url.openStream());

一旦你完成上述操作,就隻要簡單地将 InputStream 中的位元組,寫入到 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);

    }

在最後的代碼塊中,關閉這些流。

這個例子是利用 doPost 來實作的(doPost 是 HttpServlet 子類的一個方法):

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 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 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();

    }

}

結論

正如你所讀到的,用 servlet 來打開非 html 文檔相當簡單。即使是要通過防火牆也是如此。隻要設定了正确的 MIME 類型,就可以使用同樣的代碼來打開圖檔或其他多媒體檔案。當今的網際網路上包含了大量資訊,其中許多資料被存儲為非 HTML 格式。使用 servlet 能夠克服 HTML 的限制,簡單友善地向使用者傳送這些非 HTML 格式的資訊。