通过网络发送具有自定义属性的文件
我想创建一个客户端-服务器程序,允许客户端将文件以及有关该文件的一些信息(发送者名称、描述等)发送到服务器。
该文件可能非常大,因为它可以是文本、图片、音频或视频文件,因此我不想在发送之前将整个文件读入字节数组,我宁愿读取该文件以块的形式,通过网络发送它们,然后允许服务器将块附加到文件的末尾。
然而,我面临着如何最好地发送文件以及有关文件本身的一些信息的问题。我希望至少发送发件人的姓名和描述,这两者都将由用户输入到客户端程序,但这可能会在将来发生变化,因此应该灵活。
有什么好的方法可以让我“流式传输”正在发送的文件,而不是整体读取它然后发送?
I want to create a client-server program that allows the client to send a file to the server along with some information about the file (sender name, description, etc.).
The file could potentially be quite large as it could be either a text, picture, audio or video file, and because of that I do not want to have to read the whole file into a byte array before sending, I would rather read the file in blocks, sending them over the network and then allowing the server to append the blocks to the file at it's end.
However I am faced with the problem of how to best send the file along with a few bits of information about the file itself. I would like at a minimum to send the sender's name and a description both of which will be input to the client program by the user, but this may change in the future so should be flexible.
What is a good way of doing this that would also allow me to "stream" the file being sent rather than reading it in as a whole and then sending?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
套接字本质上是字节流,因此您应该不会遇到问题。我建议你有一个看起来像这样的协议。
这将允许您发送任意属性,只要总长度小于 64 KB。后面是文件,可以是任意 63 位长度,一次发送一个块。 (缓冲区为 8 KB)
如果您愿意,可以使用 Socket 发送更多文件。
阅读
Sockets are natively streams of bytes so you shouldn't have a problem there. I suggest you have a protocol which looks like this.
This will allow you to send arbitrary properties as long as the total length is less than 64 KB. Followed by the file which can be any 63-bit length and is sent a block at a time. (with a buffer of 8 KB)
The Socket can be used to send more files if you wish.
to read
您可以围绕众所周知的协议(如 FTP)构建程序。
要发送元信息,您只需创建一个具有包含该信息的唯一名称的特殊文件。然后使用 FTP 传输用户文件和元文件。
否则,再次对文件使用 FTP,您可以在手写程序的客户端-服务器流中传输元数据。
You could build up program around a well known protocol as FTP.
And to send the meta information you could just create a special file with a unique name that contains the info. Afterwards transfer both the user file and the meta file with FTP.
Otherwise, again using FTP for the file you could transfer the meta data in the client-server stream of your hand-written program.
我建议为此使用 http 协议。服务器可以使用 servlet 实现,Apache HttpClient 可用于客户端。 本文提供了一些很好的示例。您可以在同一请求中同时发送文件和参数。而且代码也很少!
I recommend using the http protocol for this. The server can be implemented using a servlet and Apache HttpClient can be used for the client. This article has some good examples. You can send both the file and the parameters in the same request. And that too with very little code!