摘要 java Servlet 編程可以很方便地將 Html 文件發(fā)送到客戶端 Web 瀏覽器。然而許多站點還答應(yīng)訪問非 HTML 格式的文檔,包括 Adobe PDF、Microsoft Word 和 Micorsoft Excel 等。事實上這些非 HTML 格式只要能用 MIME 類型表示,就可以利用 servlet 來發(fā)送。本文將以 PDF 和 Microsoft Word 文件為例,向你介紹如何使用 servlet 傳送非 HTML 格式文件,以及與防火墻交互的方法。 你只要將文件寫到 servlet 的輸出流中,就可以利用 servlet 在瀏覽器中打開一個文件。盡管這看起來非常簡單,在打開非 HTML 格式文檔(比如二進(jìn)制數(shù)據(jù)或多媒體文件)的時候,仍要注重一些要點。 首先從獲得 servlet 的輸出流開始:
ServletOutputStream out = res.getOutputStream();
互聯(lián)網(wǎng)上使用 MIME (multipurpos Internet mail extension 多目的互聯(lián)網(wǎng)郵件擴(kuò)展協(xié)議)來傳送混合格式、多媒體和二進(jìn)制數(shù)據(jù)文件。假如要在 servlet 的 response 對象中打開某個文檔,就必須設(shè)置該文檔的 MIME 類型。下面這個例子中我們將打開 PDF 文檔。
MIME 類型 Web 瀏覽器使用 MIME 類型來識別非 HTML 文檔,并決定如何顯示該文檔內(nèi)的數(shù)據(jù)。將插件 (plug-in) 與 MIME 類型結(jié)合使用,則當(dāng) Web 瀏覽器下載 MIME 類型指示的文檔時,就能夠啟動相應(yīng)插件處理此文檔。某些 MIME 類型還可以與外部程序結(jié)合使用,瀏覽器下載文檔后會啟動相應(yīng)的外部程序。
MIME 類型非常有用。它們答應(yīng) Web 瀏覽器處理不同格式的文檔,卻不需要事先嵌入相關(guān)知識。Java Servlets 可以使用 MIME 類型來向瀏覽器傳送非 HTML 文件,比如 Adobe PDF 和 Micorsoft Word。使用正確的 MIME 類型能夠保證這些非 HTML 文件被正確的插件或外部程序顯示。本文末的資料部分提供了一些網(wǎng)址,指向一些已定義 MIME 類型列表和關(guān)于 MIME 類型的文章。
PDF 文件的 MIME 類型是 "application/pdf"。要用 servlet 來打開一個 PDF 文檔,需要將 response 對象中 header 的 content 類型設(shè)置成 "application/pdf":
// MIME type for pdf doc res.setContentType( "application/pdf" );
若要打開一個 Microsoft Word 文檔,你就要將 response 對象的 content 類型設(shè)置成 "application/msword":
// MIME type for MSWord doc res.setContentType( "application/msword" );
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.
// 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);
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 //---------------------------------------------------------------
//------------------------------------------------------------ // 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;
結(jié)論 正如你所讀到的,用 servlet 來打開非 html 文檔相當(dāng)簡單。即使是要通過防火墻也是如此。只要設(shè)置了正確的 MIME 類型,就可以使用同樣的代碼來打開圖片或其他多媒體文件。當(dāng)今的互聯(lián)網(wǎng)上包含了大量信息,其中許多數(shù)據(jù)被存儲為非 HTML 格式。使用 servlet 能夠克服 HTML 的限制,簡單方便地向用戶傳送這些非 HTML 格式的信息。