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

參考文章

form實現(xiàn)文件下載

1.0前端實現(xiàn)思路

用一個from接收后臺返回的文件流。form用display為none隱藏;其中form構(gòu)造action屬性,屬性值為后臺文件下載的參數(shù)。同樣可以用display為none的input插入form中,input可以攜帶參數(shù),后臺可以用@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標(biāo)簽,初始化相關(guān)參數(shù) 
                            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代碼實現(xiàn)

后端Java代碼實現(xiàn)首先將文件讀入到數(shù)組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();
            // 設(shè)置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();
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,104評論 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,780評論 1 45
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評論 25 709
  • 1、很早就聽過《圍城》,這次因為笑來老師《財富自由之路》提到了這本書,我立即買回來并開始閱讀。跟著笑來老師的腳步,...
    阿白不急閱讀 342評論 0 0
  • 今天談?wù)勗趺床樽值涞膯栴}。 不管學(xué)哪門外語,查字典都是必不可少的,閱讀讀不懂,聽力聽不懂很大程度上是詞匯跟不上,遇...
    youjia閱讀 2,786評論 1 4

友情鏈接更多精彩內(nèi)容