HTTP获取批量文件打包下载-java(自)
所需jar包:
ant.jar
类:
1 package com.keertech.emessage.helper; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.OutputStream; 11 import java.net.ConnectException; 12 import java.net.HttpURLConnection; 13 import java.net.URL; 14 import java.util.Map; 15 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 19 import org.apache.tools.zip.ZipOutputStream; 20 21 public class ZipFile { 22 /** 23 * 批量压缩问文件 24 * @param zipFileName //压缩包文件名 25 * @param urlsMap //Map<String, String>-Map<文件重命名, 文件url> 26 * @param request 27 * @param response 28 * @throws Exception 29 */ 30 public static void bulkPackedFiles(String zipFileName,Map<String, String> urlsMap,HttpServletRequest request, HttpServletResponse response) throws Exception { 31 String fileName = zipFileName + ".zip"; 32 //在服务器端创建打包下载的临时文件 33 String relativePath = "tmp/"; //保存视频及截图的目录 34 String realPath = request.getSession().getServletContext().getRealPath("/"); //web项目部署的实际物理地址E:/tomcat/webapps/em/ 35 String directory = realPath+relativePath; //视频上传web临时文件夹 36 37 String outFilePath = directory + fileName; //将打包文件输出到服务器临时目录下,待用户下载完成后删除 38 System.out.println("输出路径:"+outFilePath); 39 40 File file = new File(outFilePath); 41 //文件输出流 42 FileOutputStream outStream = new FileOutputStream(file); 43 //压缩流 44 ZipOutputStream toClient = new ZipOutputStream(outStream); 45 toClient.setEncoding("UTF-8"); 46 zipBatchFileByStream(urlsMap, toClient); 47 toClient.close(); 48 outStream.close(); 49 downloadZip(file, response); 50 } 51 52 public static void bulkPackedFilesByStream(String zipFileName,Map<String, String> urlsMap) throws Exception { 53 String fileName = zipFileName + ".zip"; 54 //在服务器端创建打包下载的临时文件 55 String directory = "d://"; //视频上传web临时文件夹 56 57 String outFilePath = directory + fileName; //将打包文件输出到服务器临时目录下,待用户下载完成后删除 58 System.out.println("输出路径:"+outFilePath); 59 60 File file = new File(outFilePath); 61 //文件输出流 62 FileOutputStream outStream = new FileOutputStream(file); 63 //压缩流 64 ZipOutputStream toClient = new ZipOutputStream(outStream); 65 toClient.setEncoding("UTF-8"); 66 zipBatchFileByStream(urlsMap, toClient); 67 toClient.close(); 68 outStream.close(); 69 //downloadZip(file, response); 70 } 71 72 /** 73 * 压缩文件列表中的文件 74 * @param files 75 * @param outputStream 76 * @throws IOException 77 */ 78 public static void zipBatchFileByStream(Map<String, String> urlsMap, ZipOutputStream outputStream) throws Exception 79 { 80 try 81 { 82 for (String tempfileName : urlsMap.keySet()) { 83 String urlStr = urlsMap.get(tempfileName); 84 URL url=new URL(urlStr); 85 HttpURLConnection urlConn; 86 try { 87 urlConn = (HttpURLConnection)url.openConnection(); 88 urlConn.setRequestMethod("GET"); 89 urlConn.setConnectTimeout(5 * 1000); 90 int connResCode = urlConn.getResponseCode(); 91 }catch (Exception ce) { //如果连接失败则跳过该文件 92 ce.printStackTrace(); 93 continue; 94 } 95 96 97 //生成文件名 98 String fileExtension = urlStr.substring(urlStr.lastIndexOf(".")); 99 String fileName = tempfileName+fileExtension; 100 InputStream is=urlConn.getInputStream(); 101 zipFileByStream(fileName, is, outputStream); //重命名文件,并将其写入到ZIP压缩包中 102 is.close(); 103 } 104 } 105 catch(Exception e) 106 { 107 throw e; 108 } 109 } 110 111 /** 112 * 将文件写入到zip文件中 113 * @param inputFile 114 * @param outputstream 115 * @throws Exception 116 */ 117 public static void zipFileByStream(String fileName ,InputStream is, ZipOutputStream outputstream) throws Exception 118 { 119 try{ 120 121 BufferedInputStream bInStream = new BufferedInputStream(is); 122 123 org.apache.tools.zip.ZipEntry entry = new org.apache.tools.zip.ZipEntry(fileName); 124 outputstream.putNextEntry(entry); 125 126 byte[] buffer = new byte[1024]; 127 int r = 0; 128 while ((r = bInStream.read(buffer)) != -1) { 129 outputstream.write(buffer, 0, r); 130 } 131 132 outputstream.setEncoding("gbk"); //解决中文文件名乱码 133 134 // outputstream.write(inOutbyte); 135 outputstream.closeEntry(); //Closes the current ZIP entry and positions the stream for writing the next entry 136 bInStream.close(); //关闭 137 } 138 catch(IOException e) 139 { 140 throw e; 141 } 142 } 143 144 /** 145 * 下载打包的文件 146 * @param file 147 * @param response 148 */ 149 public static void downloadZip(File file,HttpServletResponse response) { 150 try { 151 // 以流的形式下载文件。 152 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); 153 byte[] buffer = new byte[fis.available()]; 154 fis.read(buffer); 155 fis.close(); 156 // 清空response 157 response.reset(); 158 159 OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); 160 response.setContentType("application/octet-stream"); 161 response.setHeader("Content-Disposition", "attachment;filename=" + file.getName()); 162 toClient.write(buffer); 163 toClient.flush(); 164 toClient.close(); 165 file.delete(); //将生成的服务器端文件删除 166 } 167 catch (IOException ex) { 168 ex.printStackTrace(); 169 } 170 } 171 }
调用
1 Map<String, String> urlsMap = new HashMap<String, String>(); 2 int photoCount = 1; 3 for (SignPhoto tempPhoto : list) { 4 //签到图片文件名:用户名+考勤时间+序号 5 String userName = sign.getUser().getName()+signDateStr+"_NO"+photoCount; 6 String photoUrl = Context.getSystemConfig().getFileRootUrl() + tempPhoto.getFile().getSavePath(); 7 urlsMap.put(userName, photoUrl); 8 photoCount++; 9 } 10 sdf = new SimpleDateFormat("yyyy-MM-dd"); 11 String zipFileName = sign.getUser().getName()+sdf.format(sign.getSignDate())+"test"; //zip包名 12 ZipFile.bulkPackedFiles(zipFileName,urlsMap,request,response); //打zip包