struts zip打包多个文件并下载

来源:互联网 发布:绝对萌域淘宝店名 编辑:程序博客网 时间:2024/05/19 07:07
downFileName=new String((DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss")+"_备份.zip").getBytes(), "ISO8859-1");ByteArrayOutputStream os = new ByteArrayOutputStream();ZipOutputStream zipOut = new ZipOutputStream(os);for (int i = 0; i < fileNames.size(); i++) {FileInputStream in = new FileInputStream(PathUtil.getWebRootPath() + "back/"+fileNames.get(i));BufferedInputStream bins = new BufferedInputStream(in, 1024);//org.apache.tools.zip.ZipEntryZipEntry entry = new ZipEntry(fileNames.get(i));zipOut.putNextEntry(entry);// 向压缩文件中输出数据   int nNumber;byte[] buffer = new byte[1024];while ((nNumber = bins.read(buffer)) != -1) {zipOut.write(buffer, 0, nNumber);}// 关闭创建的流对象   bins.close();in.close();zipOut.closeEntry();}inputStream=new ByteArrayInputStream(os.toByteArray());



sturts.xml 配置

<result name="down" type="stream">    <param name="contentType">application/zip</param>    <param name="inputName">inputStream</param>    <param name="contentDisposition">attachment;filename="${downFileName}"</param>    <param name="bufferSize">1024</param> </result>


0 0