Vue文件批量下载 Spring boot后台

2 篇文章 0 订阅
1 篇文章 0 订阅
public void downLoadFile(HttpServletResponse response, List<XbptSfProjectMaterialApp> list,XbptSfProjectApp xbptSfProjectApp) {
    List<File> files = new ArrayList<>();
    for (XbptSfProjectMaterialApp xbptSfProjectMaterialApp : list) {
        String [] fileIds = xbptSfProjectMaterialApp.getMaterialId().split(",");
        for (String id : fileIds){
            UpmsFile upmsFile = upmsFileService.queryById(id);
            File file = new File(filePath + upmsFile.getSrc());
            files.add(file);
        }
    }
    downFile(files, response,xbptSfProjectApp);
}

public HttpServletResponse downFile(List<File> list, HttpServletResponse response,XbptSfProjectApp xbptSfProjectApp) {
    try {
        String zipFiles = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyHHmm");
        String date = sdf.format(new Date());
        if (StringUtils.isBlank(xbptSfProjectApp.getProjectDeclareNum())){
            zipFiles = xbptSfProjectApp.getEntName()+date+".zip";
        }else {
            zipFiles = xbptSfProjectApp.getProjectDeclareNum()+date+".zip";
        }
        File file = new File(zipFiles);
        if (!file.exists()) {
            file.createNewFile(); //创建一个zip文件
        }
        response.reset();//清空response
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
        zipFiles(list, zipOutputStream);
        zipOutputStream.close();
        fileOutputStream.close();
        return downloadZip(file, response);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

public void zipFiles(List<File> files, ZipOutputStream outputStream) {
    int size = files.size();
    for (int i = 0; i < size; i++) {
        File file = files.get(i);
        zipFile(file, outputStream);
    }
}

/**
 * @param inputFile   //文档路径
 * @param ouputStream
 */
public void zipFile(File inputFile, ZipOutputStream ouputStream) {
    try {
        //File路径
        FileInputStream IN = new FileInputStream(inputFile);
        BufferedInputStream bins = new BufferedInputStream(IN, 1024);
        ZipEntry entry = new ZipEntry(inputFile.getName());
        ouputStream.putNextEntry(entry);
        // 向压缩文件中输出数据
        int nNumber;
        byte[] buffer = new byte[1024];
        while ((nNumber = bins.read(buffer)) != -1) {
            ouputStream.write(buffer, 0, nNumber);
        }
        // 关闭创建的流对象
        bins.close();
        IN.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public HttpServletResponse downloadZip(File file, HttpServletResponse response) {
    try {
        // 以流的形式下载文件。
        InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();

        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        // 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
        //        response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
        response.setHeader("content-disposition",
                "attachment;filename*=" + URLEncoder.encode(file.getName(), "UTF-8"));
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            File f = new File(file.getPath());
            f.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return response;
}

Vue前端接收流写法

let config = {
  headers: {
    'Content-Type': 'application/json'
  },
  responseType:'blob',//必须要写
};

this.$http.post("/upms/api/v1/itim_doc/downloadFile", this.tableSelection, config).then(res => {//tableSelection参数,我自己的是对象,后台用List接,可以看一downloadFile接口
  let data = res.data;
  let contentDisposition = res.headers['content-disposition'];
  let fileName = contentDisposition.substring(contentDisposition.indexOf('=') + 1);
  let url = window.URL.createObjectURL(new Blob([data]));
  let edik = document.createElement('a');
  edik.style.display = 'none';
  edik.href = url;
  edik.setAttribute('download', fileName);
  document.body.appendChild(edik);
  //点击下载
  edik.click();
  // 释放掉blob对象
  window.URL.revokeObjectURL(edik);
  // 下载完成移除元素
  document.body.removeChild(edik);
});
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring BootVue是两个独立的框架,分别用于后端和前端开发。Spring Boot是一个基于Spring框架的快速开发平台,可以用于构建Java应用程序的后端部分。它提供了很多开箱即用的功能和约定,使得开发者可以更专注于业务逻辑的实现。 Vue是一个用于构建用户界面的JavaScript框架,它能够通过组件化和响应式的方式构建复杂的前端应用。Vue提供了许多工具和库来简化前端开发,并且具有高效的渲染能力和优秀的性能。 在后台管理系统中,通常会使用Spring Boot作为后端框架来提供数据接口和业务逻辑的实现。通过Spring Boot的RESTful接口,可以实现前后端之间的交互和数据传输。同时,Spring Boot还可以提供安全认证、日志记录、数据库连接等功能,使得后台管理系统更加可靠和安全。 而Vue则可以用于构建后台管理系统的前端界面。Vue提供了丰富的组件库和工具,可以方便地构建用户界面。通过Vue的组件化和响应式特性,可以实现页面的动态更新和交互。同时,Vue还支持路由管理和状态管理等功能,可以更好地组织和管理前端代码。 综上所述,Spring BootVue可以很好地配合使用来开发后台管理系统。Spring Boot提供了强大的后端能力,而Vue则提供了优秀的前端开发框架,两者的结合可以使得后台管理系统更具有实用性和可扩展性。同时,Spring BootVue都具有良好的社区支持和文档资料,方便开发者学习和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhh1996075

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值