zoukankan      html  css  js  c++  java
  • 下载功能-vue

    下载功能-页面
    fileDownload(info){
    	let fileName = info['bankFileName'];
    	let filePath = info['bankFile'];
    	let postData = {'fileName':fileName,'filePath':filePath};
    	this.downloadData(this.fileDownloadUrl, postData).then(data => {
    	    if (data.size === 0) {
    	        this.msgError('文件下载出错');
    	    } else {
    	        const blob = new Blob([data]);
    	        const elink = document.createElement('a');
    	        elink.download = fileName;
    	        elink.style.display = 'none';
    	        elink.href = URL.createObjectURL(blob);
    	        document.body.appendChild(elink);
    	        elink.click();
    	        URL.revokeObjectURL(elink.href); // 释放URL 对象
    	        document.body.removeChild(elink);
    	        this.msgSuccess('文件下载成功');
    	    }
    	});
    }
    

      

    下载功能-后端
    @RequestMapping(value = "/downLoadFileTest", method = RequestMethod.POST)
    public void downLoadFileTest(HttpServletRequest request, HttpServletResponse response) {
        ResultEntity re = new ResultEntity();
        String fileName = request.getParameter("fileName");
        String filePath = request.getParameter("filePath");
        logger.info("fileName = "+fileName);
        logger.info("filePath = "+filePath);
        if (StrKit.isBlank(fileName) || StrKit.isBlank(filePath)) {
            logger.error("下载文件出错");
            return;
        }
        InputStream input = null;
        OutputStream os = null;
        try {
            logger.info("下载开始");
            String downLoadFilePath = filePath;
            File downLoadFile = new File(downLoadFilePath);
            //读取流
            input = new BufferedInputStream(new FileInputStream(downLoadFilePath));
            if (input == null) {
                logger.error("下载附件失败,请检查文件“" + downLoadFilePath + "”是否存在");
                return;
            }
            byte[] buffer = new byte[input.available()];
            input.read(buffer);
            input.close();
            // 清空response
            response.reset();
            // 重要,设置response的Header
            response.setHeader("Content-Disposition", "attachment;filename="" + new String(fileName.getBytes())+""");
            response.setHeader("Content-Length", "" + downLoadFile.length());
            //octet-stream是二进制流传输,当不知文件类型时都可以用此属性
            response.setContentType("application/octet-stream");
            //跨域请求,*代表允许全部类型
            response.setHeader("Access-Control-Allow-Origin", "*");
            //允许请求方式
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            //用来指定本次预检请求的有效期,单位为秒,在此期间不用发出另一条预检请求
            response.setHeader("Access-Control-Max-Age", "600");
            //请求包含的字段内容,如有多个可用哪个逗号分隔如下
            response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request,lang");
            //访问控制允许凭据,true为允许
            response.setHeader("Access-Control-Allow-Credentials", "true");
            //创建一个输出流,用于输出文件
            OutputStream oStream = new BufferedOutputStream(response.getOutputStream());
            //写入输出文件
            oStream .write(buffer);
            oStream .flush();
            oStream .close();
            logger.info("下载文件=" + fileName +"成功");
        } catch (Exception e) {
            logger.error("下载文件出错:" + e.getMessage(), e);
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                logger.error("下载附件失败");
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                logger.error("下载附件失败");
            }
        }
    }
    

      

  • 相关阅读:
    hdu 2191 珍惜现在,感恩生活(多重背包)
    《从Paxos到ZooKeeper分布式一致性原理与实践》学习知识导图
    你对ArrayList了解多少?
    JAVA酒店管理系统
    C#酒店管理系统
    C#图书管理系统
    java图书管理系统
    豆瓣高分JAVA书籍,你都读过吗?
    JAVA课程设计----------JAVA学生信息管理系统
    C#学生管理系统
  • 原文地址:https://www.cnblogs.com/internHe/p/14241610.html
Copyright © 2011-2022 走看看