Java根据多个文件URL打包成一个压缩包下载
直接上代码:
@GetMapping("/download")public void downloadFiles(HttpServletRequest request, HttpServletResponse response, String[] urls) {// 响应头的设置response.reset();response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");// 设置压缩包的名字// 解决不同浏览器压缩包名字含有中文时乱码的问题String downloadName = System.currentTimeMillis()+".zip";String agent = request.getHeader("USER-AGENT");try {if (agent.contains("MSIE") || agent.contains("Trident")) {downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");} else {downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");}} catch (Exception e) {e.printStackTrace();}response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");// 设置压缩流:直接写入response,实现边压缩边下载ZipOutputStream zipos = null;try {zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));zipos.setMethod(ZipOutputStream.DEFLATED); // 设置压缩方法} catch (Exception e) {e.printStackTrace();}// 循环将文件写入压缩流DataOutputStream os = null;for (int i = 0; i < urls.length; i++) {String fileName = getFileTypeByUrl(urls[i]);File file = getFileByUrl(urls[i], fileName);try {// 添加ZipEntry,并ZipEntry中写入文件流// 这里,加上i是防止要下载的文件有重名的导致下载失败zipos.putNextEntry(new ZipEntry(fileName));os = new DataOutputStream(zipos);InputStream is = new FileInputStream(file);byte[] b = new byte[100];int length = 0;while ((length = is.read(b)) != -1) {os.write(b, 0, length);}is.close();zipos.closeEntry();} catch (IOException e) {e.printStackTrace();}}// 关闭流try {os.flush();os.close();zipos.close();} catch (IOException e) {e.printStackTrace();}}//url转fileprivate File getFileByUrl(String fileUrl, String suffix) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();BufferedOutputStream stream = null;InputStream inputStream = null;File file = null;try {URL imageUrl = new URL(fileUrl);HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");inputStream = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inputStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}file = File.createTempFile("file", suffix);FileOutputStream fileOutputStream = new FileOutputStream(file);stream = new BufferedOutputStream(fileOutputStream);stream.write(outStream.toByteArray());} catch (Exception e) {} finally {try {if (inputStream != null)inputStream.close();if (stream != null)stream.close();outStream.close();} catch (Exception e) {}}return file;}//根据获取文件后缀private String getFileTypeByUrl(String url) {String suffixes = "3fr|arw|bmp|cr2|crw|dcr|dng|eps|erf|gif|icns|ico|jpeg|jpg|mos|mrw|nef|odd|orf|pdf|pef|png|ppm|ps|psd|raf|raw|svg|svgz|tif|tiff|webp|x3f|xcf|xps| 7z|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|dmg|eml|gz|img|iso|jar|lha|lz|lzma|lzo|rar|rpm|rz|tar|tar.7z|tar.bz|tar.bz2|tar.gz|tar.lzo|tar.xz|tar.Z|tbz|tbz2|tgz|tZ|tzo|xz|z|zip|aac|ac3|aif|aifc|aiff|amr|caf|flac|m4a|m4b|mp3|oga|ogg|sf2|sfark|voc|wav|weba|wma| 3g2|3gp|3gpp|avi|cavs|dv|dvr|flv|gif|m2ts|m4v|mkv|mod|mov|mp4|mpeg|mpg|mts|mxf|ogg|rm|rmvb|swf|ts|vob|webm|wmv|wtv| abw|djvu|doc|docm|docx|html|lwp|md|odt|pages|pages.zip|pdf|rst|rtf|sdw|tex|txt|wpd|wps|zabw|eps|html|key|key.zip|odp|pdf|pps|ppsx|ppt|pptm|pptx|ps|sda|swf| csv|html|numbers|numbers.zip|ods|pdf|sdc|xls|xlsm|xlsx|azw|azw3|azw4|cbc|cbr|cbz|chm|docx|epub|fb2|htm|html|htmlz|lit|lrf|mobi|odt|oeb|pdb|pdf|pml|prc|rb|rtf|snb|tcr|txt|txtz|eot|otf|ttf|woff|dwg|dxf|ai|cdr|cgm|emf|eps|pdf|ps|sk|sk1|svg|svgz|vsd|wmf|website";Pattern pat = Pattern.compile("[\\w]+[\\.](" + suffixes + ")");// 正则判断Matcher mc = pat.matcher(url);// 条件匹配String substring = "";while (mc.find()) {substring = mc.group();// 截取文件名后缀名}return substring;}
Java根据多个文件URL打包成一个压缩包下载相关推荐
- easyExcel导出excel文件并打包成zip压缩包下载
文件导出 专栏收录该内容 2 篇文章0 订阅 订阅专栏 package com.business.testExcelPort; import java.io.BufferedInputStream; ...
- 压缩base 64字符串_ftp下载多个文件,ftp下载多个文件打包成一个压缩包
在一些日常的网络批量维护工作中,经常需要使用ftp计划任务,定时上传或下载多个文件.对不太了解ftp命令和windows计划任务的新手来说,确实是一个很棘手的问题.今天我们来看看如何简单的实现这功能. ...
- FTP下载多个文件打包成一个压缩包
@RequestMapping("downloadsByplFromFTP") @ResponseBody public void downloadsByplFr ...
- 将多个excel表格打包成一个压缩包文件
上一篇文章讲解了如何生成excel文件,name这一章我们来谈谈如何将多个excel文件打包成压缩包的形式 首先看一下主函数: public static void main(String[] arg ...
- asar软件包linux,ASAR文件查看打包解包工具下载-ASAR文件查看打包解包工具v2018.07.12免费版-ucbug软件站...
ASAR文件查看打包解包工具是一款能够帮助用户对ASAR文件进行管理的工具,通过ASAR文件查看打包解包工具能够对文件进行查看.打包.解包等功能,有需要的可以下载使用. 功能介绍 electron的a ...
- java 导出表格打包zip文件下载_asyExcel导出excel并打包成zip压缩包下载
假期期间自己在家撸码,刚好用到了导出,导出来之后是多个文件,所以需要打成压缩包并下载来给客户.查阅了一些资料,把这段代码贴在这,相当于有个记录吧. package com.business.testE ...
- 批量下载文件,打包成zip压缩包
批量下载文件,用程序打成zip压缩包在下载 前台传来要下载的url数组 @RequestMapping(value = "/download",method = RequestMe ...
- web 项目中资源下载出错,或者下载的文件对应的形式不对应,web 项目下载的文件自动打包为一个压缩包
项目组中遇到一个奇怪的问题,在给手机段提供接口是,提供了一个apk文件的下载路径,华为D2手机通过这个路径下载,下载下来不是一个apk文件,而是打成了一个压缩包.而其他手机下载下来的是正常的apk包. ...
- Java 实现上传文件到FTP和从FTP下载文件
在eclipse 中创建项目,并将commons-net-3.6.jar包复制到项目中 commons-net-3.6.jar:链接:https://pan.baidu.com/s/1iz0862TX ...
最新文章
- thinkphp实现登录后返回原界面
- 有关于Matlab的regionprops函数的PixelIdxList和PixelList的一点解释
- “Assign Random Colors” is not working in 3ds Max 2015
- 地球是否健康,最新“诊断报告”来了
- python不想学了-嫌Python太慢但又不想学C/C++?来了解下JIT技术
- linux进程与它的堆栈空间
- Pycharm中配置Pyflink
- hbase中清空整张表的数据
- StringBuilder String string.Concat 字符串拼接速度再议
- 常量指针与指针常量勘误
- 计划任务列表 html,OpenWrt使用crontab执行计划任务
- 移位运算符优先级很低
- PHP异步调用实现方式
- Vue之设置背景图片
- 中国5G务实发展 向大规模商用推进
- jquery字符串转json
- HTML5权威指南 6.多媒体相关API
- Electron + React 跨平台桌面应用部署及发布
- 《About Face 3:交互设计精髓》读书笔记(一)
- 从 ftp 上下载文件、文件夹