Controller
/**
* @description: 试题篮子里的音频打包下载
* @author: zcq
* @date: 2020/11/26 6:28 下午
*/
@RequestMapping("/downloadAudioZip")
public void downloadAudioZip(@RequestBody List<AudioDownloadDto> audioDownloadDto, HttpServletResponse response) {
try {
String zipName = DateUtils.format(Field.DEFAULT_LONG_FORMAT, new Date());
FileUtil.downloadAllFilesZip(zipName, audioDownloadDto, response);
} catch (Exception e) {
Result result = Result.buildFailResult(e.getMessage());
AlertMessageUtil.packageErrorResult(result);
log.error("试题篮子里的音频打包下载失败", e);
}
}
FileUtils
import com.hwl.tool.FileUtils;
import com.hwl.tool.ListUtils;
import com.tal.search.business.model.dto.AudioDownloadDto;
import com.tal.search.core.common.contants.Constants;
import com.tal.search.core.exceptions.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @description: 文件相关工具类
* @author: zcq
* @date: 2020/11/24 08:34 下午
*/
@Slf4j
public class FileUtil {
public static final String ZIP_FILE = "/Users/zhangchuanqiang/Desktop/file.zip";
public static void downloadAllFilesZip(String zipName, List<AudioDownloadDto> downloads, HttpServletResponse response) throws ServiceException {
long beginTime = System.currentTimeMillis();
if (ListUtils.isEmpty(downloads)) {
return;
}
dealRepeatFileName(downloads);
ZipOutputStream zos = null;
try {
String downloadFilename = zipName + ".zip";
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
// 指明response的返回对象是文件流
response.setContentType("application/octet-stream");
// 设置在下载框默认显示的文件名
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
zos = new ZipOutputStream(response.getOutputStream());
// zos = new ZipOutputStream(new FileOutputStream(new File(ZIP_FILE)));
for (AudioDownloadDto download : downloads) {
String fileUrl = download.getUrl();
ZipEntry zipEntry = new ZipEntry(getFileName(download.getQueId(), download.getUrl()));
zos.putNextEntry(zipEntry);
InputStream fis = FileUtils.getInputStreamByFileUrl(fileUrl);
if (fis == null) {
continue;
}
byte[] buffer = new byte[2048];
int r;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
// 注意写完一个文件,需要关闭这个文件,再写下一个
zos.closeEntry();
zos.flush();
}
zos.finish();
printInfo(beginTime);
} catch (Exception e) {
log.error("下载音频压缩包失败 :{}", e.getMessage());
throw new ServiceException("下载音频压缩包失败");
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
log.error("ZipOutputStream close fail :{}", e.getMessage());
}
}
}
/**
* @description: 文件名重复问题
* @author: zcq
* @date: 2020/11/27 4:44 下午
*/
private static void dealRepeatFileName(List<AudioDownloadDto> downloads) {
Set<String> queIdSet = new HashSet();
for (AudioDownloadDto download : downloads) {
if (!queIdSet.add(download.getQueId())) {
// 试题重复,通过增加时间戳来去重
download.setQueId(download.getQueId() + "-" + UUID.randomUUID().toString().replace("-", ""));
}
}
}
/**
* @description: 根据文件路径获取文件的扩展名
* @author: zcq
* @date: 2020/11/26 6:18 下午
*/
private static String getFileName(String queId, String url) {
return queId + Constants.Decollator.POINT_DECOLLATOR + StringUtils.substringAfterLast(url, Constants.Decollator.POINT_DECOLLATOR);
}
private static void printInfo(long beginTime) {
log.info("file zip execute time → " + (System.currentTimeMillis() - beginTime));
}
}
资料