当前位置:   article > 正文

若依框架单文件直接下载,多文件打包下载_ruoyi下载两个文件

ruoyi下载两个文件

一、前端

handleDownload(row){
  if (row.xz == 1) {
    this.$message.warning('禁止下载')
    return
  }
  this.download('/packDownload', {'id': id})
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、后台

/**
 * 打包下载
 * @param response
 * @param zbJszlQueryDTO
 */
@Override
public void packDownload(HttpServletResponse response, UserDTO userDTO) throws Exception {
    // 获取文件的保存位置
    List<String> paths = 文件路径
    if (paths.size() == 1) {  // 直接下载
        String filePath = paths.get(0);
        filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");

        File file = new File(filePath);
        if (!file.exists()) {
            throw new Exception("文件不存在");
        }
        String suffix = filePath.substring(filePath.lastIndexOf("."));
        // 输出文件流
        writeFileToRes(response, "user" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + suffix, file);
    } else {  // 压缩之后在进行下载
        //压缩包名称(会拼上当前时间)
        String datumName = "user";
        //压缩文件
        List<String> filePathList = paths;
        File file = compressedFileToZip(datumName, filePathList);
        //输出文件流
        writeFileToRes(response, file.getName(), file);
        //删除压缩包
        if(file.exists()){
            file.delete();
        }
    }
}
// 输出文件流到response
private void writeFileToRes(HttpServletResponse response, String fileName, File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型.
    response.setContentType("application/octet-stream");
    //2.设置文件头:最后一个参数是设置下载文件名
    response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    response.addHeader("Content-Length", "" + file.length());

    //3.通过response获取ServletOutputStream对象(out)
    ServletOutputStream out = response.getOutputStream();

    int b = 0;
    byte[] buffer = new byte[1024];
    while (b != -1) {
        b = inputStream.read(buffer);
        //4.写到输出流(out)中
        out.write(buffer, 0, b);
    }
    out.flush();
    out.close();
    inputStream.close();
}
// 压缩文件
private File compressedFileToZip(String datumName, List<String> filePathList) throws Exception {
    //压缩包具体名称(拼接时间戳防止重名)
    String zipFileName = datumName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
    //生成压缩包存储地址(最后会删掉)
    String fileZip = RuoYiConfig.getProfile() + "/" + zipFileName;
    OutputStream os=null;
    ZipOutputStream zos = null ;
    File file = new File(fileZip);
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        os=new FileOutputStream(file);
        //压缩文件
        zos = new ZipOutputStream(os);
        byte[] buf = new byte[1024];
        for (String filePath : filePathList) {
            filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");
            File tempFile = new File(filePath);
            //在压缩包中添加文件夹
            //zos.putNextEntry(new ZipEntry("测试/"+tempFile.getName()));
            //直接在压缩包中添加文件
            zos.putNextEntry(new ZipEntry(tempFile.getName()));
            int len;
            FileInputStream in = new FileInputStream(tempFile);
            while ((len = in.read(buf)) != -1){
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            in.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.toString());
        throw new Exception("文件打包:"+e.getMessage());
    }finally {
        //关闭流
        if(zos != null){
            try {
                zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //关闭流
        if(os!= null){
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return file;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/594996
推荐阅读
相关标签
  

闽ICP备14008679号