springboot + vue 文件下载

后端代码

/**
* 文件下载
* @param filePath 文件名/文件根路径+文件名 如 test\test.docx
* @param uploadpath 文件存储路径   如: d:\temp
* @param uploadType 本地文件传  local
* @param response
*/
public void viewAndDownload(String filePath, String uploadpath, String uploadType, HttpServletResponse response) {
        InputStream inputStream = null;
        ServletOutputStream outputStream = null;

        try {
            String fileName;
            String bucketName;
            if (filePath.startsWith("http")) {
                fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
                String objectName;
                if ("minio".equals(uploadType)) {
                    bucketName = filePath.replace(MinioUtil.getMinioUrl(), "").split("/")[0];
                    objectName = filePath.replace(MinioUtil.getMinioUrl() + bucketName, "");
                    inputStream = MinioUtil.getMinioFile(bucketName, objectName);
                    if (inputStream == null) {
                        bucketName = "eoafile";
                        objectName = filePath.replace(OssBootUtil.getStaticDomain() + "/", "");
                        inputStream = OssBootUtil.getOssFile(objectName, bucketName);
                    }
                } else {
                    bucketName = "eoafile";
                    objectName = filePath.replace(OssBootUtil.getStaticDomain() + "/", "");
                    inputStream = OssBootUtil.getOssFile(objectName, bucketName);
                    if (inputStream == null) {
                        bucketName = filePath.replace(MinioUtil.getMinioUrl(), "").split("/")[0];
                        objectName = filePath.replace(MinioUtil.getMinioUrl() + bucketName, "");
                        inputStream = MinioUtil.getMinioFile(bucketName, objectName);
                    }
                }

                response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
            } else {
                filePath = filePath.replace("..", "");
                if (filePath.endsWith(",")) {
                    filePath = filePath.substring(0, filePath.length() - 1);
                }

                fileName = uploadpath + File.separator + filePath;
                bucketName = uploadpath + File.separator + fileName;
                File file = new File(bucketName);
                inputStream = new BufferedInputStream(new FileInputStream(fileName));
                response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
            }

            response.setContentType("application/force-download");
            outputStream = response.getOutputStream();
            if (inputStream != null) {
                byte[] buf = new byte[1024];

                int len;
                while((len = ((InputStream)inputStream).read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }

                response.flushBuffer();
            }
        } catch (IOException var22) {
            response.setStatus(404);
            this.log.error("预览文件失败" + var22.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    ((InputStream)inputStream).close();
                } catch (IOException var21) {
                    this.log.error(var21.getMessage(), var21);
                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException var20) {
                    this.log.error(var20.getMessage(), var20);
                }
            }

        }

    }

前端代码


//axios封装
export function downFile(url, parameter) {
  return axios({
    url: url,
    params: parameter,
    method: 'get',
    responseType: 'blob'
  })
}



exportData(){
   var params = this.getQueryParams();//查询条件
   delete params.entrustStartTimeSearch
   delete params.testStartTimeSearch
   this.loading = true;
   downFile(this.url.exportData,params).then((data)=>{
     if (!data) {
       this.$message.warning("导出失败")
       return
     }
     let url = window.URL.createObjectURL(new Blob([data]));
     let link = document.createElement('a');
     link.style.display = 'none';
     link.href = url;
     link.setAttribute('download', '试验设备时长统计表.xlsx');
     document.body.appendChild(link);
     link.click()
     this.$message.success("导出成功!")
     this.loading = false;
   })
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值