java把多个url压缩成zip, 前端接收返回的字符串并下载
2021/9/28 12:10:38
本文主要是介绍java把多个url压缩成zip, 前端接收返回的字符串并下载,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java把多个url压缩成zip, 前端接收返回的字符串并下载
说明: 后端把多个url转成字节数组,并合成zip供前端下载。
1. 把url的转成字节数据
public static byte[] getFileUrlByte(String fileUrl) throws BusException { try { URL url = new URL(fileUrl); HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); urlCon.setConnectTimeout(6000); urlCon.setReadTimeout(6000); int code = urlCon.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { throw new BusException("文件读取失败"); } InputStream is = url.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[BYTE_LENGTH]; int length = 0; while ((length = is.read(bytes)) != -1) { outputStream.write(bytes, 0, length); } outputStream.close(); is.close(); return outputStream.toByteArray(); } catch (IOException e) { log.error("文件下载异常: {}", ExceptionUtil.stacktraceToString(e)); throw new BusException(String.format("文件下载失败, %s", e.getMessage())); } }
2.把多个文件流合成zip输出流
public static byte[] regularZipByte(List<FileInfo> fromFiles) throws BusException { byte[] arr = new byte[0]; try { Random random = new Random(); @Cleanup ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @Cleanup ZipOutputStream zipOut = new ZipOutputStream(outputStream); // 如果有重复的名称, 则重命名 Set<String> fileNameSet = Sets.newHashSet(); for (FileInfo fileUrl : fromFiles) { String fileUrlTemp = fileUrl.getFileUrl(); int index = fileUrlTemp.lastIndexOf(ProjectConstant.SLASH); String urlPrefix = fileUrl.getFileUrl().substring(0, index + 1); String originalFileName = fileUrl.getFileUrl().substring(index + 1); String entryName = originalFileName; if (fileNameSet.contains(entryName)) { if (entryName.contains(ProjectConstant.POINT)) { String[] split = entryName.split(ProjectConstant.POINT_ESCAPE); entryName = split[0] + random.nextInt(9999) + ProjectConstant.POINT + split[1]; } else { entryName += random.nextInt(9999); } } byte[] fileUrlByte = getFileUrlByte(urlPrefix + URLEncoder.encode(originalFileName, "UTF-8")); fileNameSet.add(entryName); zipOut.putNextEntry(new ZipEntry(entryName)); zipOut.write(fileUrlByte); } zipOut.finish(); outputStream.flush(); arr = outputStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); throw new BusException("下载异常"); } return arr; }
3. 前端接受接口返回的字节数组, 并解析下载
testBtn4 () { axios({ method: 'get', url: 'http://localhost:端口/地址' }).then((data) => { var raw = window.atob(data.data) var uInt8Array = new Uint8Array(raw.length) for (var i = 0; i < raw.length; i++) { uInt8Array[i] = raw.charCodeAt(i) } const blob = new Blob([uInt8Array], { type: 'application/octet-stream' }) const link = document.createElement('a') link.style.display = 'none' link.href = URL.createObjectURL(blob) link.download = '导出文件' + Date.parse(new Date()) + '.zip' document.body.appendChild(link) link.click() document.body.removeChild(link) }) },
备注:
后端返回的格式
这篇关于java把多个url压缩成zip, 前端接收返回的字符串并下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求