下載功能-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("下載附件失敗");
        }
    }
}

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章