Java实现批量下载选中文件功能
1.在action中定义变量
privateListdownLoadPaths=newArrayList ();//存储选中文件的下载地址 privateOutputStreamres; privateZipOutputStreamzos; privateStringoutPath; privateStringlessionIdStr;//选中文件ID拼接的字符串 privateStringfileName;//浏览器下载弹出框中显示的文件名
分别给出get和set方法
2. 主方法
/** *下载多个文件:压缩成zip * *@return *@throwsException */ publicStringdownLoadLessionsZip(){ downLoadPaths.clear(); StringfirstFileName="";//第一个文件的文件名 ListfileVos=newLinkedList (); if(StringUtils.isNotEmpty(lessionIdStr)){ intend=lessionIdStr.lastIndexOf(","); if(end>0){ if(end==lessionIdStr.length()-1){ lessionIdStr=lessionIdStr.substring(0,end); } String[]ids=lessionIdStr.split(","); for(inti=0;i fileVos){ byte[]buf=newbyte[8192]; intlen; for(DownLoadFileVofileVo:fileVos){ Filefile=newFile(fileVo.getFileSavePath()); if(!file.isFile()) continue; ZipEntryze=newZipEntry(fileVo.getFileName() +fileVo.getFileSavePath().substring( fileVo.getFileSavePath().lastIndexOf("."))); try{ zos.putNextEntry(ze); BufferedInputStreambis=newBufferedInputStream( newFileInputStream(file)); while((len=bis.read(buf))>0){ zos.write(buf,0,len); } bis.close(); zos.closeEntry(); }catch(IOExceptione){ e.printStackTrace(); } } } //预处理 publicvoidpreProcess(StringfirseFileName){ StringzipName="【批量下载】"+firseFileName+"等.zip"; Stringfilename=""; try{ filename=newString(zipName.getBytes("GBK"),"8859_1"); }catch(UnsupportedEncodingExceptione1){ e1.printStackTrace(); } this.fileName=filename; HttpServletResponseresponse=ServletActionContext.getResponse(); try{ res=response.getOutputStream(); //清空输出流(在迅雷下载不会出现一长窜) response.reset(); //设定输出文件头 response.setHeader("Content-Disposition","attachment;fileName=" +filename); response.setContentType("application/zip"); zos=newZipOutputStream(res); }catch(IOExceptione){ e.printStackTrace(); } } //后处理 publicvoidafterProcess(){ try{ if(zos!=null){ zos.close(); } if(res!=null){ res.close(); } }catch(IOExceptione){ e.printStackTrace(); } }
3.在struts.xml中配置
//class值为bean.xml中配置的bean 204//表示响应执行成功,但没有数据返回,浏览器不用刷新,不用导向新页面
用到的jar包
总结
以上所述是小编给大家介绍的Java实现批量下载选中文件功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。