Controller

  1. /**
  2. * @description: 试题篮子里的音频打包下载
  3. * @author: zcq
  4. * @date: 2020/11/26 6:28 下午
  5. */
  6. @RequestMapping("/downloadAudioZip")
  7. public void downloadAudioZip(@RequestBody List<AudioDownloadDto> audioDownloadDto, HttpServletResponse response) {
  8. try {
  9. String zipName = DateUtils.format(Field.DEFAULT_LONG_FORMAT, new Date());
  10. FileUtil.downloadAllFilesZip(zipName, audioDownloadDto, response);
  11. } catch (Exception e) {
  12. Result result = Result.buildFailResult(e.getMessage());
  13. AlertMessageUtil.packageErrorResult(result);
  14. log.error("试题篮子里的音频打包下载失败", e);
  15. }
  16. }

FileUtils

  1. import com.hwl.tool.FileUtils;
  2. import com.hwl.tool.ListUtils;
  3. import com.tal.search.business.model.dto.AudioDownloadDto;
  4. import com.tal.search.core.common.contants.Constants;
  5. import com.tal.search.core.exceptions.ServiceException;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.commons.lang.StringUtils;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.URLEncoder;
  12. import java.util.HashSet;
  13. import java.util.List;
  14. import java.util.Set;
  15. import java.util.UUID;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipOutputStream;
  18. /**
  19. * @description: 文件相关工具类
  20. * @author: zcq
  21. * @date: 2020/11/24 08:34 下午
  22. */
  23. @Slf4j
  24. public class FileUtil {
  25. public static final String ZIP_FILE = "/Users/zhangchuanqiang/Desktop/file.zip";
  26. public static void downloadAllFilesZip(String zipName, List<AudioDownloadDto> downloads, HttpServletResponse response) throws ServiceException {
  27. long beginTime = System.currentTimeMillis();
  28. if (ListUtils.isEmpty(downloads)) {
  29. return;
  30. }
  31. dealRepeatFileName(downloads);
  32. ZipOutputStream zos = null;
  33. try {
  34. String downloadFilename = zipName + ".zip";
  35. downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
  36. // 指明response的返回对象是文件流
  37. response.setContentType("application/octet-stream");
  38. // 设置在下载框默认显示的文件名
  39. response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
  40. zos = new ZipOutputStream(response.getOutputStream());
  41. // zos = new ZipOutputStream(new FileOutputStream(new File(ZIP_FILE)));
  42. for (AudioDownloadDto download : downloads) {
  43. String fileUrl = download.getUrl();
  44. ZipEntry zipEntry = new ZipEntry(getFileName(download.getQueId(), download.getUrl()));
  45. zos.putNextEntry(zipEntry);
  46. InputStream fis = FileUtils.getInputStreamByFileUrl(fileUrl);
  47. if (fis == null) {
  48. continue;
  49. }
  50. byte[] buffer = new byte[2048];
  51. int r;
  52. while ((r = fis.read(buffer)) != -1) {
  53. zos.write(buffer, 0, r);
  54. }
  55. fis.close();
  56. // 注意写完一个文件,需要关闭这个文件,再写下一个
  57. zos.closeEntry();
  58. zos.flush();
  59. }
  60. zos.finish();
  61. printInfo(beginTime);
  62. } catch (Exception e) {
  63. log.error("下载音频压缩包失败 :{}", e.getMessage());
  64. throw new ServiceException("下载音频压缩包失败");
  65. } finally {
  66. try {
  67. if (zos != null) {
  68. zos.close();
  69. }
  70. } catch (IOException e) {
  71. log.error("ZipOutputStream close fail :{}", e.getMessage());
  72. }
  73. }
  74. }
  75. /**
  76. * @description: 文件名重复问题
  77. * @author: zcq
  78. * @date: 2020/11/27 4:44 下午
  79. */
  80. private static void dealRepeatFileName(List<AudioDownloadDto> downloads) {
  81. Set<String> queIdSet = new HashSet();
  82. for (AudioDownloadDto download : downloads) {
  83. if (!queIdSet.add(download.getQueId())) {
  84. // 试题重复,通过增加时间戳来去重
  85. download.setQueId(download.getQueId() + "-" + UUID.randomUUID().toString().replace("-", ""));
  86. }
  87. }
  88. }
  89. /**
  90. * @description: 根据文件路径获取文件的扩展名
  91. * @author: zcq
  92. * @date: 2020/11/26 6:18 下午
  93. */
  94. private static String getFileName(String queId, String url) {
  95. return queId + Constants.Decollator.POINT_DECOLLATOR + StringUtils.substringAfterLast(url, Constants.Decollator.POINT_DECOLLATOR);
  96. }
  97. private static void printInfo(long beginTime) {
  98. log.info("file zip execute time → " + (System.currentTimeMillis() - beginTime));
  99. }
  100. }

资料