博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA文件批量下载打成压缩包
阅读量:4136 次
发布时间:2019-05-25

本文共 4414 字,大约阅读时间需要 14 分钟。

/**     *      * @param request     * @param response     * @param filePathList   文件路径集合     */    @ApiOperation(value = "文件批量下载")    @PostMapping("/downLoad/zip")    public void download(@RequestBody List
filePathList, HttpServletRequest request, final HttpServletResponse response) {
List
resultFileList = new ArrayList<>(); File file = null; try {
for (String filePath:filePathList) {
String fileName = filePath.substring(filePath.lastIndexOf("/")+1); log.info("文件路径转一下格式"); filePath = URLDecoder.decode(filePath, "utf-8"); log.info("保存的路径名称"); String destFile = "/home/data/" + fileName; log.info("1. 先从资源服务器下载 文件"); HttpUtil.downloadFile(filePath , destFile); log.info("2. 返回file"); file = new File(destFile); resultFileList.add(file); } log.info("3. 把文件压缩进去"); File zipFile = new File("/home/data/批量下载.zip"); zipFiles(resultFileList,zipFile); log.info("4. 把压缩包的文件流返回前端"); FileDownload.downFile(request, response, "批量下载.zip", zipFile); } catch (Exception e) {
e.printStackTrace(); } }/** * * @param srcFiles 要压缩的文件 * @param zipFile 压缩后的压缩包 */public static void zipFiles(List
srcFiles, File zipFile) {
// 判断压缩后的文件存在不,不存在则创建 if (!zipFile.exists()) {
try {
zipFile.createNewFile(); } catch (IOException e) {
e.printStackTrace(); } } // 创建 FileOutputStream 对象 FileOutputStream fileOutputStream = null; // 创建 ZipOutputStream ZipOutputStream zipOutputStream = null; // 创建 FileInputStream 对象 FileInputStream fileInputStream = null; try {
// 实例化 FileOutputStream 对象 fileOutputStream = new FileOutputStream(zipFile); // 实例化 ZipOutputStream 对象 zipOutputStream = new ZipOutputStream(fileOutputStream); // 创建 ZipEntry 对象 ZipEntry zipEntry = null; // 遍历源文件数组 int i = 1; for (File srcFile:srcFiles) {
// 将源文件数组中的当前文件读入 FileInputStream 流中 fileInputStream = new FileInputStream(srcFile); // 实例化 ZipEntry 对象,源文件数组中的当前文件,加个i防止有重名文件压缩失败 zipEntry = new ZipEntry("[" + i + "]" +srcFile.getName()); zipOutputStream.putNextEntry(zipEntry); // 该变量记录每次真正读的字节个数 int len; // 定义每次读取的字节数组 byte[] buffer = new byte[1024]; while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len); } i++; } zipOutputStream.closeEntry(); zipOutputStream.close(); fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) {
e.printStackTrace(); } }public static void downFile(HttpServletRequest request, HttpServletResponse response, String filename, File file) throws IOException {
// 文件存在才下载 if (file.exists()) {
OutputStream out = null; FileInputStream in = null; try {
// 1.读取要下载的内容 in = new FileInputStream(file); // 2. 告诉浏览器下载的方式以及一些设置 // 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefo要求是iso-8859-1编码 String agent = request.getHeader("user-agent"); if (agent.toLowerCase().contains("Firefox".toLowerCase())) {
filename = new String(filename.getBytes("UTF-8"), "iso-8859-1"); } else {
filename = URLEncoder.encode(filename, "UTF-8"); } // 设置下载文件的mineType,告诉浏览器下载文件类型 String mineType = request.getServletContext().getMimeType(filename); response.setContentType(mineType); // 设置一个响应头,无论是否被浏览器解析,都下载 response.setHeader("Content-disposition", "attachment; filename=" + filename); // 将要下载的文件内容通过输出流写到浏览器 out = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len); } } catch (IOException e) {
e.printStackTrace(); } finally {
if (out != null) {
out.close(); } if (in != null) {
in.close(); } } } }

转载地址:http://psxvi.baihongyu.com/

你可能感兴趣的文章
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>
Jackson Tree Model Example
查看>>
常用js收集
查看>>
如何防止sql注入
查看>>
springmvc传值
查看>>
在Eclipse中查看Android源码
查看>>
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>