亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

02vue+axios+form實現文件下載(附Java實現代碼)

標簽:
Vue.js

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


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消