Java实现批量下载文件的示例代码
更新时间:2023年10月27日 10:51:16 作者:西瓜喵
这篇文章主要为大家详细介绍了Java如何实现批量下载文件,并以压缩输出流的形式返回前端,文中的示例代码讲解详细,需要的小伙伴可以参考一下
我需要调取第三方接口的数据存到本地服务器上,然后在以输出流的形式响应
zipUtil(工具类,直接复制即可,这个是我从别的博主那里复制来的,亲测有效)
public class ZipUtil { private static Logger logger = LoggerFactory.getLogger(ZipUtils.class); // 目录标识判断符 public static final String PATCH = "/"; // 基目录 public static final String BASE_DIR = ""; // 缓冲区大小 private static final int BUFFER = 2048; // 字符集 public static final String CHAR_SET = "GBK"; /** * * 描述: 压缩文件 * @author wanghui * @created 2017年10月27日 * @param fileOutName * @param files * @throws Exception */ public static void compress(String fileOutName, List<File> files) throws Exception { try { FileOutputStream fileOutputStream = new FileOutputStream(fileOutName); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); zipOutputStream.setEncoding(CHAR_SET); if (files != null && files.size() > 0) { for (int i = 0,size = files.size(); i < size; i++) { compress(files.get(i), zipOutputStream, BASE_DIR); } } // 冲刷输出流 zipOutputStream.flush(); // 关闭输出流 zipOutputStream.close(); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述:压缩文件并进行Base64加密 * @author wanghui * @created 2017年10月27日 * @param files * @return * @throws Exception */ public static String compressToBase64(List<File> files) throws Exception { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(bos); zipOutputStream.setEncoding(CHAR_SET); if (files != null && files.size() > 0) { for (int i = 0,size = files.size(); i < size; i++) { compress(files.get(i), zipOutputStream, BASE_DIR); } } // 冲刷输出流 zipOutputStream.flush(); // 关闭输出流 zipOutputStream.close(); byte[] data = bos.toByteArray(); return new String(Base64.encodeBase64(data)); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述: 压缩 * @author wanghui * @created 2017年10月27日 * @param srcFile * @param zipOutputStream * @param basePath * @throws Exception */ public static void compress(File srcFile, ZipOutputStream zipOutputStream, String basePath) throws Exception { if (srcFile.isDirectory()) { compressDir(srcFile, zipOutputStream, basePath); } else { compressFile(srcFile, zipOutputStream, basePath); } } /** * * 描述:压缩目录下的所有文件 * @author wanghui * @created 2017年10月27日 * @param dir * @param zipOutputStream * @param basePath * @throws Exception */ private static void compressDir(File dir, ZipOutputStream zipOutputStream, String basePath) throws Exception { try { // 获取文件列表 File[] files = dir.listFiles(); if (files.length < 1) { ZipEntry zipEntry = new ZipEntry(basePath + dir.getName() + PATCH); zipOutputStream.putNextEntry(zipEntry); zipOutputStream.closeEntry(); } for (int i = 0,size = files.length; i < size; i++) { compress(files[i], zipOutputStream, basePath + dir.getName() + PATCH); } } catch (Exception e) { throw new Exception(e.getMessage(), e); } } /** * * 描述:压缩文件 * @author wanghui * @created 2017年10月27日 * @param file * @param zipOutputStream * @param dir * @throws Exception */ private static void compressFile(File file, ZipOutputStream zipOutputStream, String dir) throws Exception { try { // 压缩文件 ZipEntry zipEntry = new ZipEntry(dir + file.getName()); zipOutputStream.putNextEntry(zipEntry); // 读取文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int count = 0; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOutputStream.write(data, 0, count); } bis.close(); zipOutputStream.closeEntry(); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述: 文件Base64加密 * @author wanghui * @created 2017年10月27日 上午9:27:38 * @param srcFile * @return * @throws Exception */ public static String encodeToBASE64(File srcFile) throws Exception { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 读取文件 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)); int count = 0; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bis.close(); byte[] base64Data = Base64.encodeBase64(bos.toByteArray()); if (null == base64Data) { bos.close(); return null; } bos.close(); return new String(base64Data, CHAR_SET); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述: 文件Base64解密 * @author wanghui * @created 2017年10月27日 * @param destFile * @param encodeStr * @throws Exception */ public static void decodeToBase64(File destFile, String encodeStr) throws Exception { try { byte[] decodeBytes = Base64.decodeBase64(encodeStr.getBytes()); ByteArrayInputStream bis = new ByteArrayInputStream(decodeBytes); // 读取文件 FileOutputStream fileOutputStream = new FileOutputStream(destFile); int count = 0; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { fileOutputStream.write(data, 0, count); } fileOutputStream.close(); bis.close(); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述: 解压缩 * @author wanghui * @created 2017年10月27日 * @param srcFileName * @param destFileName * @throws Exception */ @SuppressWarnings("unchecked") public static void decompress(String srcFileName, String destFileName) throws Exception { try { ZipFile zipFile = new ZipFile(srcFileName); Enumeration<ZipEntry> entries = zipFile.getEntries(); File destFile = new File(destFileName); InputStream inputStream = null; while(entries.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry)entries.nextElement(); String dir = destFile.getPath() + File.separator + zipEntry.getName(); File dirFile = new File(dir); if (zipEntry.isDirectory()) { dirFile.mkdirs(); } else { fileProber(dirFile); inputStream = zipFile.getInputStream(zipEntry); decompressFile(dirFile, inputStream); } } zipFile.close(); } catch (Exception e) { throw new Exception(e.getMessage(),e); } } /** * * 描述: 解压文件 * @author wanghui * @created 2017年10月27日 * @param destFile * @param inputStream * @throws Exception */ private static void decompressFile(File destFile, InputStream inputStream) throws Exception { try { // 文件输入流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)); int count = 0; byte data[] = new byte[BUFFER]; while ((count = inputStream.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.close(); inputStream.close(); } catch (Exception e) { throw new Exception(e.getMessage(), e); } } /** * * 描述:文件探测 * @author wanghui * @created 2017年10月27日 * @param dirFile */ private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } } /** * 递归删除目录下的所有文件及子目录下所有文件 * @param dir 将要删除的文件目录 * @return boolean Returns "true" if all deletions were successful. * If a deletion fails, the method stops attempting to * delete and returns "false". */ public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); //递归删除目录中的子目录下 for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); }
Controller层
public void bathDownScript(HttpServletResponse response, String paths) throws Exception { String uuid = UUID.randomUUID().toString().replaceAll("-",""); //1.新建文件夹,用来存放下载下来的文件,最后进行压缩 File folder = new File("/tmp/"+uuid); System.out.println(folder.getName()); if (!folder.exists() && !folder.isDirectory()) { boolean flag = folder.mkdirs(); } //2.遍历读取文件放入文件夹中 String[] path= paths.split(","); Configuration conf = new Configuration(); conf.set("fs.defaultFS", HdfsUtils.HDFSURL); List<File> files = new ArrayList<File>(); for(String huePath:path){ //通过hdfs读取文件 byte[] res = HdfsUtils.readFile(conf,huePath); //定义文件名称 String fileName = huePath.substring(huePath.lastIndexOf("/")); //定义文件路径 File f = new File("/tmp/"+uuid+fileName); InputStream in = new ByteArrayInputStream(res); byte[] buff = new byte[1024]; FileOutputStream out = new FileOutputStream(f); try { int i = in.read(buff); while (i != -1) { out.write(buff, 0, buff.length); out.flush(); i = in.read(buff); } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } files.add(f); } //3.响应前端数据 String zipName = "myfile.zip"; response.setHeader("content-type", "application/octet-stream"); response.setCharacterEncoding("utf-8"); // 设置浏览器响应头对应的Content-disposition response.setHeader("Content-disposition", "attachment;filename=" + new String(zipName.getBytes("gbk"), "iso8859-1")); ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream()); zipOutputStream.setEncoding(ZipUtil.CHAR_SET); if (CollectionUtils.isEmpty(files) == false) { for (int i = 0,size = files.size(); i < size; i++) { ZipUtil.compress(files.get(i), zipOutputStream, ZipUtil.BASE_DIR); } } // 冲刷输出流 zipOutputStream.flush(); // 关闭输出流 zipOutputStream.close(); //4.下载完删除deleteDir boolean flag = ZipUtil.deleteDir(folder); System.out.println("删除文件:"+flag); }
以上就是Java实现批量下载文件的示例代码的详细内容,更多关于Java批量下载文件的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot普通类获取spring容器中bean的操作
这篇文章主要介绍了SpringBoot普通类获取spring容器中bean的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-09-09java9新特性Reactive Stream响应式编程 API
这篇文章主要为大家介绍了java9新特性响应式编程API的特点详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-03-03
最新评论