背景:根据浏览器的不同,下载文件需要的插件也有所不同,同时在下载个数的也有要求,比如IE浏览器,下载文件的个数如果超过50,则会出现问题。
以下介绍一个不依赖任何插件。跨浏览器的实现是方式。
import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.Collection;/** * 批量下载附件。 * @param fileId 附件Id * @param fileName 下载文件的文件名,可选 * @return 附件的数据流 * @throws BusinessException */@GET@Path("file/batchDownload/{fileIds}")public Response batchDownloadBySecret(@PathParam("fileIds") String fileIds, @QueryParam("zipFileName") String zipFileName) throws BusinessException {String nofileMsg = ResourceUtil.getString("fileupload.document.FileNoFound");// 解析fileIdList longFileIds = new ArrayList<>();if (Strings.isNotBlank(fileIds)) {String[] fileIdArray = fileIds.split(",");for (String id : fileIdArray) {longFileIds.add(Long.parseLong(id));}}// 获取所有文件对象List fileList = new ArrayList();for (Long longFileId : longFileIds) { // V3XFile 是各个公司自己的文件POV3XFile v3xfile = getFileManager().getV3XFile(longFileId);if (v3xfile == null) {//文件数据库中对应的记录已经删除,返回对应的信息throw new javax.ws.rs.NotFoundException(nofileMsg);}//创建文件String filename = v3xfile.getFilename();Date createDate = v3xfile.getCreateDate(); // 获取服务器的临时目录。各个项目取值不一File tmpDirFile = getFileManager().getFile(v3xfile.getId(), createDate); // 拿到临时目录下的文件if (tmpDirFile == null) {//物理文件已经删除,返回对应的信息throw new BusinessException(nofileMsg);}String realFileName = SystemEnvironment.getSystemTempFolder() + File.separator + filename;File realFile = new File(realFileName); // 复制文件到指定目录。各个项目使用的工具类不一,FileUtil.copyFile(tmpDirFile, realFile);fileList.add(realFile);}String name;if (StringUtils.isBlank(zipFileName)) {name = "responseZipFile";} else {name = zipFileName;}try {name = URLEncoder.encode(name+".zip", "UTF-8");} catch (UnsupportedEncodingException e) {log.error("batchDownload - url encode file name error ", e);}response.setHeader("Content-Disposition ", "attachment;filename=" + name );try {zip(fileList,response.getOutputStream());} catch (IOException e) {log.error("batchDownload - compress zip file error ", e);}for (File f : fileList) {try {f.delete();} catch (Exception e) {log.error("batchDownload - delete temporary file", e);}}return Response.ok().build();}private static final String OUTPUT_ENCODING = java.nio.charset.Charset.defaultCharset().toString();private void zip(Collection files, OutputStream zipOutPutStream) throws IOException, FileNotFoundException {byte[] b = new byte[1024];ZipOutputStream zipout = null;try {zipout = new ZipOutputStream(zipOutPutStream);zipout.setEncoding(OUTPUT_ENCODING);for (File file : files) {if (!file.exists()) {throw new FileNotFoundException("File not found:" + file.getAbsolutePath());}String filename = file.getName();if (file.isFile()) {InputStream in = new FileInputStream(file);try {ZipEntry ze = new ZipEntry(filename);ze.setUnixMode(644);// 解决linux乱码zipout.putNextEntry(ze);int len = 0;while ((len = in.read(b)) > 0) {zipout.write(b, 0, len);}} finally {in.close();}}}} catch (FileNotFoundException ex) {throw ex;} catch (IOException ex1) {throw ex1;} finally {if (zipout != null) {try {zipout.close();} catch (IOException ex) {}}}} 前端调用
// JSP代码如下 // JS调用 function batchDownloadFun(){ $("#batchDownload").bind("click",function(){ if($(".eachSelect:checked").size() == 0){ //$.alert("至少选择一项数据,当前已选择0项"); $.alert("${ctp:i18n('collaboration.summary.label.batchdown')}"); return; }else{ var $obj = $(".eachSelect:checked"); var ids=""; for (var i = 0; i < $obj.size(); i ++) { ids = ids + $obj[i].value; if(i<$obj.size()-1){ ids = ids +","; } } var url = _ctxPath+"/rest/attachment/file/batchDownload/"+ids+"?zipFileName="+Date.now(); $("#batchDownloadResult").attr("href",url); $("#batchDownloadResultSpan").trigger("click"); } }); } | 留言与评论(共有 0 条评论) “” |