在 WebApp 中创建和下载大型 ZIP(来自多个 BLOB)的最佳实践 [英] Best Practices to Create and Download a huge ZIP (from several BLOBs) in a WebApp

查看:15
本文介绍了在 WebApp 中创建和下载大型 ZIP(来自多个 BLOB)的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的 Web 应用程序执行大量文件下载.

I will need to perform a massive download of files from my Web Application.

这显然是一个长期运行的动作(它将每年使用一次[-per-customer]),所以时间不是问题(除非它超时,但我可以通过创建某种形式的保持活动心跳来处理).我知道如何创建一个隐藏的 iframe 并将它与 content-disposition:attachment 一起使用以尝试下载文件而不是在浏览器中打开它,以及如何实例化用于绘制进度表的客户端-服务器通信;

It is obviously expected to be a long-running action (it'll be used once-per-year[-per-customer]), so the time is not a problem (unless it hits some timeout, but I can handle that by creating some form of keepalive heartbeating). I know how to create an hidden iframe and use it with content-disposition: attachment to attempt to download the file instead of opening it inside the browser, and how to instance a client-server communication for drawing a progress meter;

下载的实际大小(和文件数量)未知,但为简单起见,我们实际上可以将其视为 1GB,由 100 个文件组成,每个文件 10MB.

The actual size of the download (and the number of files) is unknown, but for simplicity we can virtually consider it as 1GB, composed of 100 files, each 10MB.

由于这应该是一个单击操作,我的第一个想法是将所有文件分组,同时从数据库中读取它们,在一个动态生成的 ZIP 中,然后要求用户保存 ZIP.

Since this should be a one-click operation, my first thought was to group all the files, while reading them from the database, in a dynamically generated ZIP, then ask the user to save the ZIP.

问题是:在从 Web 应用程序中的多个小字节数组创建大型存档时,最佳做法是什么,已知的缺点和陷阱是什么?

可以随机分成:

  • 应该将每个字节数组转换为物理临时文件,还是可以将它们添加到内存中的 ZIP 中?
  • 如果是,我知道我将不得不处理可能的名称相等(它们可以在数据库的不同记录中具有相同的名称,但不在同一个文件系统或 ZIP 中):是否还有其他可能的问题想到的(假设文件系统总是有足够的物理空间)?
  • 因为我不能依靠有足够的 RAM 来在内存中执行整个操作,所以我猜应该在发送给用户之前创建 ZIP 并将其提供给文件系统;有没有什么不同的方法(例如使用 websocket),比如询问用户在哪里保存文件,然后开始从服务器到客户端的持续数据流(Sci-Fi 我猜)?
  • 如果您想到任何其他相关的已知问题或最佳做法,我们将不胜感激.
  • should each byte array be converted in a physical temp file, or can they be added to the ZIP in memory ?
  • if yes, I know I'll have to handle the possible equality of names (they can have the same name in different records in the database, but not inside the same file system nor ZIP): are there any other possible problems that come to mind (assuming the file system always has enough physical space) ?
  • since I can't rely on having enough RAM to perform the whole operation in memory, I guess the ZIP should be created and fed to the file system before being sent to the user; is there any way to do it differently (eg with websocket), like asking the user where to save the file, and then starting a constant flow of data from the server to client (Sci-Fi I guess) ?
  • any other related known problems or best practices that cross your mind would be greatly appreciated.

推荐答案

对于无法一次性放入内存的大型内容,将内容从数据库流式传输到响应中.

For large content that won't fit in memory at once, stream the content from the database to the response.

这种事情其实很简单.您不需要 AJAX 或 websockets,可以通过用户单击的简单链接流式传输大文件下载.现代浏览器有不错的下载管理器,有自己的进度条——为什么要重新发明轮子?

This kind of thing is actually pretty simple. You don't need AJAX or websockets, it's possible to stream large file downloads through a simple link that the user clicks on. And modern browsers have decent download managers with their own progress bars - why reinvent the wheel?

如果为此从头开始编写 servlet,请访问数据库 BLOB,获取其输入流并将内容复制到 HTTP 响应输出流.如果你有 Apache Commons IO 库,你可以使用 IOUtils.copy(),否则你可以自己做.

If writing a servlet from scratch for this, get access to the database BLOB, getting its input stream and copy content through to the HTTP response output stream. If you have Apache Commons IO library, you can use IOUtils.copy(), otherwise you can do this yourself.

可以使用 ZipOutputStream.在响应输出流上创建其中一个(来自 servlet 或您的框架提供给您的任何内容),然后从数据库中获取每个 BLOB,首先使用 putNextEntry() ,然后如前所述流式传输每个 BLOB.

Creating a ZIP file on the fly can be done with a ZipOutputStream. Create one of these over the response output stream (from the servlet or whatever your framework gives you), then get each BLOB from the database, using putNextEntry() first and then streaming each BLOB as described before.

潜在的陷阱/问题:

  • 根据下载大小和网络速度,请求可能需要很长时间才能完成.防火墙等可能会阻止并提前终止请求.
  • 希望您的用户在请求这些文件时使用的是良好的公司网络.在远程/狡猾/移动连接上会更糟糕(如果在下载 1.9G 或 2.0G 后掉线,用户必须重新开始).
  • 它会给您的服务器带来一些负担,尤其是压缩巨大的 ZIP 文件.如果这是一个问题,可能值得在创建 ZipOutputStream 时关闭/关闭压缩.
  • 超过 2GB(或者 4GB)的 ZIP 文件可能会在某些 ZIP 程序中出现问题.我认为最新的 Java 7 使用 ZIP64 扩展,所以这个版本的 Java 可以正确地编写巨大的 ZIP,但是客户端是否有支持大 zip 文件的程序?我以前肯定遇到过这些问题,尤其是在旧的 Solaris 服务器上

这篇关于在 WebApp 中创建和下载大型 ZIP(来自多个 BLOB)的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆