欢迎光临
我们一直在努力

如何用 servlet 打开非 HTML 格式的文档-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

如何用 servlet 打开非 html 格式的文档
一种向 web 客户端发送非 html 格式文档的简单方法

摘要
java servlet 编程可以很方便地将 html 文件发送到客户端 web 浏览器。然而许多站点还允许访问非 html 格式的文档,包括 adobe pdf、microsoft word 和 micorsoft excel 等。事实上这些非 html 格式只要能用 mime 类型表示,就可以利用 servlet 来发送。本文将以 pdf 和 microsoft word 文件为例,向你介绍如何使用 servlet 传送非 html 格式文件,以及与防火墙交互的方法。
你只要将文件写到 servlet 的输出流中,就可以利用 servlet 在浏览器中打开一个文件。尽管这看起来非常简单,在打开非 html 格式文档(比如二进制数据或多媒体文件)的时候,仍要注意一些要点。
首先从获得 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 dont 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 datas 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 – dont 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 格式的信息。

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何用 servlet 打开非 HTML 格式的文档-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章