使用 servlet 从服务器下载 Excel 文件 [英] Downloading Excel file from server using servlets

查看:51
本文介绍了使用 servlet 从服务器下载 Excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器端有一个 Excel 文件.如何使用 servlet 在客户端浏览器上显示它?

I have an Excel file on the server side. How I can display it on client side browser using servlets?

提前致谢.

推荐答案

重点:只需以某种方式获取它的 InputStream(FileInputStream 是合适的)并编写它到响应的 OutputStream 通常Java IO 方式.这基本上就是全部.您只需要注意设置正确的响应标头,以便浏览器了解如何处理它.Content-Type 标头将指示 webbrowser 它是什么类型的文件,以便浏览器知道使用哪个应用程序打开它.

To the point: just get an InputStream of it somehow (FileInputStream is suitable) and write it to the OutputStream of the response the usual Java IO way. That's basically all. You'll only need to take care that you set the right response headers, so that the browser understands what to do with it. The Content-Type header will instruct the webbrowser what kind of file it is so that the browser knows which application to use to open it.

这是一个启动示例:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
    File file = new File("/path/to/files", filename);

    response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
    response.setHeader("Content-Length", file.length());
    response.setHeader("Content-Disposition", "inline; filename="" + file.getName() + """);

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException ignore) {}
        if (input != null) try { input.close(); } catch (IOException ignore) {}
    }
}

web.xml 中的这个 servlet 映射到 /files/*url-pattern 上,这样你就可以通过http://example.com/contextname/files/filename.xls.

Map this servlet in web.xml on an url-pattern of /files/* so that you can get the excel file by http://example.com/contextname/files/filename.xls.

如果它实际上是一个 xlsx 文件,默认情况下它不能被普通的 servletcontainer 识别(ServletContext#getMimeType() 然后将返回 application/octet-stream 而不是所需的 xlsx 内容类型),那么您还需要将以下条目添加到 web.xml 中:

If it's actually an xlsx file, which isn't by default recognized by the average servletcontainer yet (the ServletContext#getMimeType() would then return application/octet-stream instead of the desired xlsx content type), then you need to add the following entry to the web.xml as well:

<mime-mapping>
    <extension>xlsx</extension>
    <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>

有关文件 servlet 的更高级示例,您可以找到 这篇文章也很有用,它也支持每个下载的简历.

For a more advanced example of a file servlet you may find this article useful as well, it supports under each download resumes as well.

这篇关于使用 servlet 从服务器下载 Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆