继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

02vue+axios+form实现文件下载(附Java实现代码)

幕布斯6054654
关注TA
已关注
手记 1264
粉丝 219
获赞 1011

1.0前端实现思路

用一个from接收后台返回的文件流。form用display为none隐藏;其中form构造action属性,属性值为后台文件下载的参数。同样可以用display为none的input插入form中,input可以携带参数,后台可以用@requestParam接收。
前端具体代码如下:

<!doctype html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title></head><body>
    <div id="app" class="m-5">
        <input type="button" value="下载" @click="handlerClick">
    </div>
    <script>
        new Vue({                    el: '#app',                    data: {                        files: []
                    },                    methods: {                        handlerClick: function () {                            //自定义form标签,初始化相关参数 
                            var form = document.createElement("form");                            var access_token = "1756467474";
                            form.setAttribute("style",                                "display:none");
                            form.setAttribute("method", "get");                            var params = {};
                            params.Authorization = access_token;
                            form.setAttribute("header",
                                params);                            var path =                                'E:\\_ex_workplace\\zxsbWeb\\esgov-zxsb-zxsbweb\\src\\pages\\selfDetection.vue';                            var input = document.createElement('input');
                            input.setAttribute('type', 'hidden');
                            input.setAttribute('name', 'path');
                            input.setAttribute('value', path);
                            form.append(input);
                            form.setAttribute("action",                                "http://127.0.0.1:8778/download"
                            );
                            form.setAttribute("target", "_blank");                            var body = document.createElement("body");
                            body.setAttribute("style", "display:none");                            document.body.appendChild(form);
                            form.submit();
                            form.remove();
                        }
                    }    </script></body></html>

02.java代码实现

后端Java代码实现首先将文件读入到数组buffer中,然后用response获取输出流,输出流将buffer写出即可。

@GetMapping("/download")    public void download(@RequestParam("path") String path, HttpServletResponse response) {
        System.out.println(path);        try {            // path是指欲下载的文件的路径。
            File file = new File(path);            // 取得文件名。
            String filename = file.getName();            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();            // 以流的形式下载文件。
            InputStream fis;
            fis = new BufferedInputStream(new FileInputStream(path));            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();            // 清空response
            response.reset();            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



作者:exmexm
链接:https://www.jianshu.com/p/a0fb5b1e63b3


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP