Loading... ## 前言 拖了好久的私有云项目有了新进展,实现了原先是摆设的文件批量下载功能。 利用Java将选择的文件进行压缩后下载,大文件还没有测试,几百MB的文件压缩下载起来还是挺快的,大概几秒就会开始下载。 ## 代码 ```java try { file_names = request.getParameterValues("file_box"); if(!file_names.equals(null)) { FileInputStream in = null; OutputStream out = null; /* for(int i=0;i<file_names.length;i++) { response.setContentType(getServletContext().getMimeType(file_names[i])); //设置Content-Disposition response.setHeader("Content-Disposition", "attachment;filename="+file_names[i]); //读取目标文件,通过response将目标文件写到客户端 //获取目标文件的绝对路径 String fullFileName = config.getUp_path()+"/"+file_names[i]; //System.out.println(fullFileName); //设置响应头,控制浏览器下载该文件 response.setHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(fullFileName,"UTF-8").replaceAll("\\+", "%20")); //读取要下载的文件,保存到文件输入流 in = new FileInputStream(fullFileName); //创建输出流 out = response.getOutputStream(); //创建缓冲区 byte buffer[] = new byte[1024]; int len = 0; //循环将输入流中的内容读取到缓冲区当中 while(0<=(len=in.read(buffer))){ //输出缓冲区的内容到浏览器,实现文件下载 out.write(buffer, 0, len); } //关闭文件输入输出流 out.close(); in.close(); } */ //将文件名转化为File数组 File []fullFileName = new File[file_names.length]; for(int i=0;i<file_names.length;i++) { response.setContentType(getServletContext().getMimeType(file_names[i])); //设置Content-Disposition response.setHeader("Content-Disposition", "attachment;filename="+file_names[i]); //读取目标文件,通过response将目标文件写到客户端 //获取目标文件的绝对路径 fullFileName[i] = new File(config.getUp_path()+"/"+file_names[i]); } //压缩文件 File zipfile = new File(config.getUp_path()+"/" + "zipfile.zip") ; if (!zipfile.exists()) { try { zipfile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件已创建"); } else { System.out.println("文件已存在"); } zipFiles(fullFileName, zipfile); //设置响应头,控制浏览器下载该文件 response.setHeader("content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode("zipfile.zip","UTF-8").replaceAll("\\+", "%20")); //读取要下载的文件,保存到文件输入流 in = new FileInputStream(zipfile); //创建输出流 out = response.getOutputStream(); //创建缓冲区 byte buffer[] = new byte[1024]; int len = 0; //循环将输入流中的内容读取到缓冲区当中 while(0<=(len=in.read(buffer))){ //输出缓冲区的内容到浏览器,实现文件下载 out.write(buffer, 0, len); } //关闭文件输入输出流 out.close(); in.close(); //删除压缩文件 zipfile.delete(); } } catch (Exception e) { // TODO: handle exception } } ``` #### 压缩代码 ```java public static void zipFiles(File[] srcfile,File zipfile){ byte[] buf=new byte[1024]; try { //ZipOutputStream类:完成文件或文件夹的压缩 ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipfile)); for(int i=0;i<srcfile.length;i++){ FileInputStream in=new FileInputStream(srcfile[i]); out.putNextEntry(new ZipEntry(srcfile[i].getName())); int len; while((len=in.read(buf))>0){ out.write(buf,0,len); } out.closeEntry(); in.close(); } out.close(); } catch (Exception e){} } ``` ## 待改进 - 小文件还好,服务器压缩比较快,文件大一点了就会等待较长时间。考虑边下载边压缩。 - 对部分手机浏览器支持不友好,不能成功下载zip。 Last modification:January 2nd, 2020 at 10:33 pm © 允许规范转载 Support If you think my article is useful to you, please feel free to appreciate ×Close Appreciate the author Sweeping payments
Comment here is closed