springboot文件上传下载的一天

1.zip打包批量下载文件,拿到即可用

@Slf4j
public class ZipDownloadUtil {
    /**
     * zip打包下载
     */
    public void zipDownload(HttpServletResponse response, String wordPath, String zipFileName, List<File> fileList) {
        //生成缓存文件夹名
        String zipPath = wordPath + File.separator + "cache" + File.separator + zipFileName;
        try {
            //创建zip输出流
            try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath))) {
                //声明文件集合用于存放文件
                byte[] buffer = new byte[1024];
                //将文件放入zip压缩包
                for (int i = 0; i < fileList.size(); i++) {
                    File file = fileList.get(i);
                    try (FileInputStream fis = new FileInputStream(file)) {
                        out.putNextEntry(new ZipEntry(file.getName()));
                        int len;
                        // 读入需要下载的文件的内容,打包到zip文件
                        while ((len = fis.read(buffer)) > 0) {
                            out.write(buffer, 0, len);
                        }
                        out.closeEntry();
                    }
                }
            }
            //下载zip文件
            downFile(response,wordPath, zipFileName);
        } catch (Exception e) {
            log.error("文件下载出错", e);
        } finally {
            // zip文件也删除
            List<File> fileList1 =new ArrayList<>();
            fileList1.add(new File(zipPath));
            deleteFile(fileList1);
        }
    }

    /**
     * 文件下载
     */
    public void downFile(HttpServletResponse response,String wordPath, String zipFileName) {

        // zip文件路径 自己的路径自定义哈
        String path = wordPath + File.separator + "cache" + File.separator + zipFileName;
        try {
            File file = new File(path);

            if (file.exists()) {
                try (InputStream ins = new FileInputStream(path);
                     BufferedInputStream bins = new BufferedInputStream(ins);
                     OutputStream outs = response.getOutputStream();
                     BufferedOutputStream bouts = new BufferedOutputStream(outs)) {
                    response.setContentType("application/x-download");
                    response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
                    int bytesRead = 0;
                    byte[] buffer = new byte[1024];
                    while ((bytesRead = bins.read(buffer, 0, 1024)) != -1) {
                        bouts.write(buffer, 0, bytesRead);
                    }
                    bouts.flush();
                }
            }
        } catch (Exception e) {
            log.error("文件下载出错", e);
        }
    }

    /**
     * 删除文件
     */
    public void deleteFile(List<File> fileList) {
        for (File file : fileList) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}

 

posted @ 2021-10-14 17:36  Demonssss  阅读(38)  评论(0)    收藏  举报