springboot 批量生成条形码图片,并下载

博客涉及MySQL数据库相关内容,包含实现类和工具类,聚焦于信息技术领域数据库方面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控制层:
@ApiOperation(value = "批量下载条形码", notes = "批量下载条形码")
@GetMapping(value = "/downloadZipFile")
public JsonViewObject downloadFileLocal(@ApiParam(value = "下载文件url", required = true) @RequestParam("urlList") List<String> urlList,
                                        @RequestParam("zipFileName") String name,
                                        HttpServletRequest request,
                                        HttpServletResponse response) {
    JsonViewObject jsonView = JsonViewObject.newInstance();
    try {
        equipmentAccountService.downLoadFileList(response,urlList,name);
        jsonView.success("下载成功");
    } catch (Exception e) {
        log.error("文件下载失败!", e);
        return jsonView.fail("文件下载失败!");
    }
    return jsonView.success("文件下载成功!");
}

实现类:

@Override
public void downLoadFileList(HttpServletResponse response,List<String> urlList,String name) {
    List<File> fileList = new ArrayList<>();
    for (String filePath:urlList
         ) {
        File file = new File(filePath);
        fileList.add(file);
    }

    //下载
    ZipDownloadUtil.zipDownload(response,name,fileList);

}

工具类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDownloadUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZipDownloadUtil.class);

    /**
     * 获取当前系统的临时目录
     */
    private static final String FILE_PATH = System.getProperty("java.io.tmpdir") + File.separator;

    private static final int ZIP_BUFFER_SIZE = 8192;

    /**
     * zip打包下载
     *
     * @param response
     * @param zipFileName
     * @param fileList
     */
    public static void zipDownload(HttpServletResponse response, String zipFileName, List<File> fileList) {
        zipFileName = zipFileName+".zip";
        // zip文件路径
        String zipPath = FILE_PATH + 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, zipFileName);
        } catch (Exception e) {
            logger.error("文件下载出错", e);
        } finally {
            // zip文件也删除
            fileList.add(new File(zipPath));
            //deleteFile(fileList);
        }
    }


    /**
     * 文件下载
     *
     * @param response
     * @param zipFileName
     */
    private static void downFile(HttpServletResponse response, String zipFileName) {
        try {
            String path = FILE_PATH + zipFileName;
            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[ZIP_BUFFER_SIZE];
                    while ((bytesRead = bins.read(buffer, 0, ZIP_BUFFER_SIZE)) != -1) {
                        bouts.write(buffer, 0, bytesRead);
                    }
                    bouts.flush();
                }
            }
        } catch (Exception e) {
            logger.error("文件下载出错", e);
        }
    }

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

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值