基于Spring MVC的文件传输

基于Spring MVC的文件传输

在上网的过程中,经常会发现点击一个按钮或者链接之后,出现文件的保存。这就是服务器向你发送文件。在工作中也遇到了这样的问题。这里简单的总结一下。
实现文件下载最简单的方法就是在网页上做超级链接,如<a href="xxxxx/xxxx.jpg">点击下载</a>。

(1) 这样会把服务器上的目录资源暴露给用户。(主要)
(2) 每当修改服务器上的文件位置的时候,都需要对应修改。当然你可以使用jsp等动态网页的方法。

还可以通过发送请求,服务器返回文件的方法来实现。

可采用:1. RequestDispatcher的方式;2.文件流输出的方式下载。


需要设置的属性有 content-type, content-disposition

MIME协议分析链接

content-type分析

其中,Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)  
服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示,如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上attachment:
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.txt");
备注:这样浏览器会提示保存还是打开,即使选择打开,也会使用相关联的程序比如记事本打开,而不是IE直接打开了。

例子1RequestDispatcher的方式
    public static void fileDownloadDispatcher(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filePath = "/fileName";// 即将下载的文件的相对路径
        String fileDisplay = URLEncoder.encode("fileDisplay", "UTF-8");// 下载文件时显示的文件保存名称
        response.setContentType("application/x-download");// 设置为下载application/x-download
        response.addHeader("Content-Disposition", "attachment;filename=" + fileDisplay);
        try {
            RequestDispatcher dis = request.getRequestDispatcher(filePath);
            if (dis != null) {
                dis.forward(request, response);
            }
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

例子2文件流输出的方式

public static void fileDownloadStream(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = "/fileName";// 即将下载的文件的相对路径
        String fileDisplay = URLEncoder.encode("fileDisplay", "UTF-8");// 下载文件时显示的文件保存名称
        response.reset();
        response.setContentType("application/x-download");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileDisplay + "\"");
        OutputStream responseStream = response.getOutputStream();
        InputStream fis = new FileInputStream(filePath);
        DataInputStream dis = new DataInputStream(fis);
        byte[] buffer = new byte[1024 * 10];
        int n = 0;
        try {
            while ((n = fis.read(buffer)) != -1) {
                responseStream.write(buffer, 0, n);
                responseStream.flush();
            }
        } catch (IOException e) {
        } finally {
            if (responseStream != null) {
                responseStream.flush();
                responseStream.close();
            }
            if (dis != null) {
                dis.close();
            }
        }
    }

参考

http://www.cnblogs.com/brucejia/archive/2012/12/24/2831060.html
http://blog.csdn.net/bripengandre/article/details/2192982
http://blog.csdn.net/cs_ghy/article/details/8142354

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值