将文件从一台主机传输到另一台主机并检查其完整性

发布于 2024-12-14 20:53:36 字数 212 浏览 2 评论 0原文

我的大学作业是创建服务器-客户端对,客户端可以将文件传输到服务器。还应该进行完整性检查以查看文件是否未损坏。我已经使用 DigestInputStream 和 DigestOutputStream 类实现了实际的文件传输,它们在传输时计算文件的哈希码。现在我的问题是将客户端计算的哈希发送到服务器,服务器必须将其与服务器计算的哈希进行比较。我需要某种协议的想法来发送实际的文件数据、其哈希码和文件名。提前致谢。

I've a university assignment to create a server-client pair, where the client can transfer a file to server. Also there should be an integrity check to see if the file is not corrupt. I've implemented the actual file transfer, using the DigestInputStream and DigestOutputStream classes, that calculate the hash code of the file at transfer time. Now my problem is to send the hash calculated by the client to server, where the server must compare it to the hash calculated by server. I need an idea of some kind of protocol to send the actual file data, its hash code and file name. Thanks in advance.

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

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

发布评论

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

评论(2

蝶舞 2024-12-21 20:53:36

使用 Socket API。

然后可以采取以下步骤:

  1. 从客户端连接到服务器;
  2. 让客户端发送计算出的哈希值;
  3. 服务器根据计算出的哈希值验证哈希值;
  4. 服务器向客户端发回“成功”消息。

有大量关于 Socket 以及如何设置基本的客户端-服务器通信的教程。

It is proabably easiest to implement using the Socket API.

Then you can take the following steps:

  1. Connect to the server from the client;
  2. Have the client send the calculated hash;
  3. Server validates the hash against its calculated one;
  4. Server sends back a "success" message to the client.

There are plenty of tutorials available on Socket and how to setup basic client-server communication.

握住你手 2024-12-21 20:53:36

您可以使用数据输出/输入流

DataOutput out = ...
String fileName = ...
byte[] bytes = ...
int hash = ...

out.writeUTF(fileName);
out.writeInt(bytes.length);
out.write(bytes);
out.writeInt(hash);
out.flush();

You can use DataOutput/InputStream

DataOutput out = ...
String fileName = ...
byte[] bytes = ...
int hash = ...

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