通过网络发送文件时如何指定文件结尾

发布于 2024-10-31 14:21:00 字数 254 浏览 9 评论 0原文

我的 servlet 使用 FileInputStream.read() 从二进制文件读取。如果到达文件末尾,则返回 -1。然后,它通过响应流将文件的字节发送到客户端。

然后我想通过响应流发送文件的 md5 哈希字节。我应该如何将文件结尾与文件的 md5 分开,以便客户端知道哪些字节是哪些?我无法发送 -1 字节,因为这样流就会停止工作。

我是否可以发送任何其他字节来表示文件结束,我知道这些字节不可能位于实际文件内,因此不可能在文件实际结束之前发出信号结束?

My servlet reads from a binary file using the FileInputStream.read(). This returns -1 if end of file is reached. It then sends the bytes of the file to the client over the response stream.

I want to then send the bytes of a md5 hash of the file over the response stream. How should i split the end of file from the md5 of the file so that the client knows which bytes are which? I can't send a -1 byte because then the stream stops working.

Are there any other bytes i can send to signal end of file that i know could not possibly be inside the actual file, and so not possibly signal the end of the file before it actually ends?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2024-11-07 14:21:00

假设您可以相信文件不会更改,我会读取该文件一次并计算 MD5 哈希值,将其设置为标头,然后将文件作为完整的正文写出。对于客户来说,这可能比将身体分成两部分更容易处理。

如果文件足够小,您可以在读取/散列它时将其写入 ByteArrayOutputStream ,以避免读取两次 - 但如果文件很大,您可能不想接受那次记忆打击。

另一种选择是将哈希值存储在文件系统中 - 让写入文件的任何内容首先负责对其进行哈希处理。这样你只需要对它进行哈希一次;如果需要的话,您可以在阅读时随时对它进行散列,以验证散列。

Assuming you can rely on the file not to change, I would read the file once and compute the MD5 hash, set that as a header, and then write the file out as the complete body. That's likely to be easier for the client to handle than splitting the body into two parts.

If the file is small enough, you could write it into a ByteArrayOutputStream as you read/hash it, to avoid having to read it twice - but if the file is large, you probably don't want to take that memory hit.

Another option is to store the hash in the file system - make whatever's writing the file in the first place responsible for hashing it. That way you only need to hash it once; you could always hash it as you read it to verify the hash if necessary.

笑梦风尘 2024-11-07 14:21:00

其他答案都很好 - 我只是提这个给你另一个观点。您可以将 MD5 哈希设置为 servlet 命名空间中的单独资源。

例如,您的客户端请求“path/download-file”来下载文件,并请求“path/download-file.md5”来获取md5。计算 MD5 的代码可以在下载文件时执行此操作,并写入文件系统,或将哈希值存储在缓存中(可能是持久的)。

在其基本形式中,这假设客户端将在下载文件之前下载文件。 md5。您可以进行检查以确保情况确实如此。另一种方法是按需计算 md5,但如果客户端在文件之前请求 md5,则会出现您希望避免的双重读取效率低下的情况。强制在下载文件后请求 md5 可以避免这种情况。

The other answers are good - I just mention this to give you another viewpoint. You can make the MD5 hash a separate resource in your servlet namespace.

E.g. your client requests "path/download-file" to download a file, and "path/download-file.md5" to get the md5. Your code that computes the MD5 can do so while the file is being downloaded, and write to the file system, or store the hash in a cache (possibly persistent.)

In its basic form, this assumes that the client will download the file before the md5. You could put in checks to ensure this is the case. The alternative is to compute the md5 on demand, but if the client requests the md5 before the file, then you get the double-read inefficiency that you were hoping to avoid. Mandating that the md5 is requested after the file is downloaded avoids this.

苏别ゝ 2024-11-07 14:21:00

首先发送 MD5 哈希值,因为它的长度已知。

Send the MD5 hash first since it has a known length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文