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

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

问题描述

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



提前感谢

解决方案

要点:只需要获得一个 InputStream FileInputStream 是合适的)并写它通过 OutputStream noreferrer> Java IO 的方式。基本上都是这样您只需要注意设置正确的响应标头,以便浏览器了解如何处理。 Content-Type 头将指示webbrowser是什么样的文件,以便浏览器知道要使用哪个应用程序打开它。



这是一个开始示例:

  protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { 
String filename = URLDecoder.decode(request.getPathInfo(),UTF-8);
文件文件=新建文件(/ 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]; (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){}
}
}

映射这个servlet在 web.xml url-pattern / files / * ,以便您可以通过 http://example.com/contextname/files/filename.xls 获取excel文件。



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

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

有关文件servlet的更高级示例,您可能会发现这篇文章也很有用,它也支持每个下载简历。


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

Thanks in advance.

解决方案

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.

Here's a kickoff example:

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) {}
    }
}

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.

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>

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天全站免登陆