/*** * obtain outputstream * * @param uploadifyFile 文件对象 * @param out * @throws Exception */ public void beachDownloadUploadify(List<UploadifyFile> uploadifyFiles, OutputStream out) { if (UtilGet.listIsEmpty(uploadifyFiles)) { // change UploadifyFile to outputstream Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(Constants.FORMATNAME); Random r = new Random(); String time =formatter.format(currentTime); String tempMulit = Constants.TEMP_DOWNLOAD + time + r.nextDouble()+r.nextLong()+r.nextLong()+r.nextInt();//临时文件目录(做压缩文件用) String mulitPath = tempMulit+Constants.ZIPFILE;//压缩文件目录 File tempDownload = new File(tempMulit); if (!tempDownload.exists() || !tempDownload.isDirectory()) { tempDownload.mkdirs(); } try { File zipFiel = new File(mulitPath); ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(zipFiel); zipOutput.setEncoding("GBK"); //将文件写入压缩文件夹 for (UploadifyFile uploadifyFile : uploadifyFiles) { String filePath = Constants.BASEPATH + Constants.SEP + uploadifyFile.getFilePath() + Constants.SEP + uploadifyFile.getRegisterName() + uploadifyFile.getRegisterExt(); File inputFile = new File(filePath); zipOutput.putArchiveEntry(new ZipArchiveEntry(uploadifyFile.getOriginalFileFullName())); FileInputStream in = new FileInputStream(inputFile); int b; while ((b = in.read()) != -1) { zipOutput.write(b); } in.close(); } zipOutput.closeArchiveEntry(); zipOutput.close(); if (null != tempDownload && tempDownload.isDirectory()) { FileUtils.deleteDirectory(tempDownload); } File file = new File(mulitPath); FileInputStream fis = new FileInputStream(file); BufferedInputStream buff = new BufferedInputStream(fis); byte[] b = new byte[10240]; long k = 0; while (k < file.length()) { int j = buff.read(b, 0, 10240); k += j; out.write(b, 0, j); } out.flush(); if (null != buff) { buff.close(); } if (null != out) { out.close(); } if(null!=zipFiel&&zipFiel.isFile()){ FileUtils.deleteQuietly(zipFiel); } } catch (Exception e) { log.warn(e); } } }
组装文件对象:
@Override @SuppressWarnings("unchecked") public void beachFileDownload(String ruleStr, OutputStream out) throws Exception { String hqlTemp = StringUtils.replace(ruleStr, ";", "','"); List<AttachFile> list = (List<AttachFile>) baseManager.getAll(" requireFileCode in ('" + hqlTemp + "') "); List<UploadifyFile> uploadifies = new ArrayList<UploadifyFile>(); if (UtilGet.isCheckList(list)) { for (AttachFile attachFile : list) { if (null != attachFile) { UploadifyFile uploadifyFile = new UploadifyFile(); uploadifyFile.setFileCode(attachFile.getRequireFileCode()); uploadifyFile.setRegisterName(StringUtils.substring(attachFile.getPhysicalFile(), 0, StringUtils.lastIndexOf(attachFile.getPhysicalFile(), "."))); uploadifyFile.setRegisterExt(StringUtils.substring(attachFile.getPhysicalFile(), StringUtils.lastIndexOf(attachFile.getPhysicalFile(), "."))); uploadifyFile.setFilePath(StringUtils.substring(attachFile.getFileUrl(), 0, StringUtils.lastIndexOf(attachFile.getFileUrl(), attachFile.getPhysicalFile()) - 1)); uploadifyFile.setOriginalFileFullName(attachFile.getFileName()); uploadifies.add(uploadifyFile); } } DownloadUploadifyUtil downloadUploadifyUtil = new DownloadUploadifyUtil(); downloadUploadifyUtil.beachDownloadUploadify(uploadifies, out); } }
controller 层 调用
/**** * 批量下载 * @param attachFileRecord * @param request * @param response */ @RequestMapping(method =RequestMethod.GET, value = PBDOWNLOAD, params = "method=beathDownload") public void beachDownload(AttachFileRecord attachFileRecord, HttpServletRequest request, HttpServletResponse response) { String ids = request.getParameter(ID); try { if (StringUtils.isNotBlank(ids)) { int i = StringUtils.split(ids,";").length; response.setContentType("text/html; charset=GBK"); String fileName = DownloadUploadifyUtil.strByteGB("档案库文档["+i+"].zip", request); response.setContentType("application/x-msdownload"); OutputStream out=response.getOutputStream(); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); attachFileService.beachFileDownload(ids,out); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
相关推荐
Java批量下载文件可以使用Java的文件输入输出流来实现。首先,需要在Servlet中获取文件列表,然后使用循环将文件一个个下载到本地桌面。具体实现代码如下: ```java protected void doGet(HttpServletRequest req, ...
### Spring Boot中使用Java实现文件批量下载与压缩功能 #### 一、背景介绍 在Web应用开发中,经常需要提供给用户批量下载的功能,尤其是在文件管理或者文档分享等场景下。传统的做法是一次只下载一个文件,这不仅...
以上就是使用Java实现FTP批量下载文件及解压的主要步骤。这个过程涵盖了FTP连接、文件操作和压缩/解压缩技术,是Java中常见的文件处理任务。在实际应用中,还需要考虑错误处理、线程安全以及可能的性能优化。
在Java开发中,文件批量上传是一项常见的功能,尤其在企业级应用中,用户可能需要上传大量数据或文件。本教程将介绍如何利用SWF(Simple Workflow)和EXT库来实现这一功能。SWF是一个用于创建富互联网应用程序的前端...
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。 public class ZipDemo extends JFrame{ JFileChooser fileChooser; //文件选择器 JList fileList; //待...
在Java编程环境中,将多个Excel文件打包成ZIP文件并实现下载功能是一项常见的需求,尤其是在数据导出或批量处理场景中。下面将详细讲解这个过程涉及的主要知识点。 首先,我们需要了解Java中的`java.util.zip`包,...
在Java编程中,批量下载和将多个文件打包成ZIP格式是一项常见的需求,特别是在处理大量数据或文件时。这里,我们看到一个示例展示了如何在Java中实现这一功能。首先,让我们详细了解一下涉及到的关键知识点。 1. **...
批量下载是指一次性发起多个下载请求,例如下载一系列图片、文件或者应用程序的更新包。这种方式可以有效地利用网络带宽,减少用户等待时间,并降低服务器压力。批量下载管理器通常会提供暂停、恢复、取消下载等功能...
综上所述,实现“java将数据导入多个excel并压缩”的功能需要对Java的文件操作、Excel处理、文件压缩以及Web开发有深入的理解。通过Apache POI库处理Excel,结合Java内置的压缩API,我们可以创建一个高效且可扩展的...
Java 实现多个文件压缩成压缩包是指使用 Java 语言将多个文件压缩成一个压缩包的过程。在这个过程中,我们可以使用 Java 的内置类库 java.util.zip,来实现文件的压缩和解压缩操作。 在 Java 中,我们可以使用 ...
Java OSS批量下载并压缩为ZIP代码实例是使用Java语言实现在OSS(Object Storage Service)中批量下载文件并将其压缩为ZIP文件的示例代码。下面将对该代码实例中的知识点进行详细解释。 1. Java OSS简介 OSS...
在本教程中,我们将深入探讨如何使用SpringBoot框架与MinIO进行集成,以实现文件的上传和下载功能。SpringBoot以其简洁的配置和强大的功能深受Java开发者喜爱,而MinIO则是一个高性能的对象存储服务器,适合处理大...
在Java中,我们可以使用`java.net.URL`和`java.io`包中的类来实现文件的下载。通过建立URL对象,创建连接,然后使用`InputStream`和`OutputStream`读取和写入数据,将远程文件内容写入本地文件。例如,可以使用`...
如果需要打包多个目录或者有特定的文件过滤需求,可以通过添加更多的`<fileset>`来实现。例如,可以这样打包两个不同的目录: ```xml **/*"/> **/*"/> ``` 此外,Ant还支持其他高级特性,如设置压缩...
然后,它读取文件内容,逐块写入到`ServletOutputStream`,实现文件的下载。 总的来说,"文件以及图片压缩zip并下载.zip"是一个包含多种类型文件(如文本文件和图片)的压缩包,利用Java的`java.util.zip`库可以...
为了更好地理解JSch的使用,你可以参考提供的压缩包文件中的文档,如"JSch - Java实现的SFTP(文件上传详解篇)",这个文档可能包含了更多详细的示例和注意事项。在进行SFTP开发时,确保遵循最佳实践,确保数据安全...
- 递归处理子目录:若想对整个包含多个子目录的项目进行反编译,可以使用`-r`选项,例如:`jad -r -d srctree/**/*.class`将递归地处理`tree`目录及其所有子目录下的`.class`文件,并根据类所在的包名称创建对应的...
在这个过程中,我们可以使用 JAVA 的 URL 连接和 ZIP 压缩技术来实现文件的下载和打包。 在这个例子中,我们定义了一个工具类 `UrlFilesToZip`,它包含两个主要方法:`getImageFromURL` 和 `readInputStream`。`...
标题"windows下批量下载远程文件.zip"指出,这个压缩包提供了一个在Windows系统下批量下载网络文件的解决方案。它可能包含一个或多个脚本、程序或配置文件,帮助用户自动化地从互联网获取多个文件。 描述中提到,该...