通过网络发送具有自定义属性的文件

发布于 2024-10-18 02:16:52 字数 305 浏览 1 评论 0原文

我想创建一个客户端-服务器程序,允许客户端将文件以及有关该文件的一些信息(发送者名称、描述等)发送到服务器。

该文件可能非常大,因为它可以是文本、图片、音频或视频文件,因此我不想在发送之前将整个文件读入字节数组,我宁愿读取该文件以块的形式,通过网络发送它们,然后允许服务器将块附加到文件的末尾。

然而,我面临着如何最好地发送文件以及有关文件本身的一些信息的问题。我希望至少发送发件人的姓名和描述,这两者都将由用户输入到客户端程序,但这可能会在将来发生变化,因此应该灵活。

有什么好的方法可以让我“流式传输”正在发送的文件,而不是整体读取它然后发送?

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 技术交流群。

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

发布评论

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

评论(3

睡美人的小仙女 2024-10-25 02:16:52

套接字本质上是字节流,因此您应该不会遇到问题。我建议你有一个看起来像这样的协议。

这将允许您发送任意属性,只要总长度小于 64 KB。后面是文件,可以是任意 63 位长度,一次发送一个块。 (缓冲区为 8 KB)

如果您愿意,可以使用 Socket 发送更多文件。

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
Properties fileProperties = new Properties();
File file = new File(filename);

// send the properties
StringWriter writer = new StringWriter();
fileProperties.store(writer, "");
writer.close();
dos.writeUTF(writer.toString());

// send the length of the file
dos.writeLong(file.length());

// send the file.
byte[] bytes = new byte[8*1024];
FileInputStream fis = new FileInputStream(file);
int len;
while((len = fis.read(bytes))>0) {
    dos.write(bytes, 0, len);
}
fis.close();
dos.flush();

阅读

DataInputStream dis = new DataInputStream(socket.getInputStream());
String propertiesText = dis.readUTF();
Properties properties = new Properties();
properties.load(new StringReader(propertiesText));
long lengthRemaining = dis.readLong();
FileOutputStream fos = new FileOutputStream(outFilename);
int len;
while(lengthRemaining > 0 
   && (len = dis.read(bytes,0, (int) Math.min(bytes.length, lengthRemaining))) > 0) {
      fos.write(bytes, 0, len);
      lengthRemaining -= len;
}
fos.close();

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.

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
Properties fileProperties = new Properties();
File file = new File(filename);

// send the properties
StringWriter writer = new StringWriter();
fileProperties.store(writer, "");
writer.close();
dos.writeUTF(writer.toString());

// send the length of the file
dos.writeLong(file.length());

// send the file.
byte[] bytes = new byte[8*1024];
FileInputStream fis = new FileInputStream(file);
int len;
while((len = fis.read(bytes))>0) {
    dos.write(bytes, 0, len);
}
fis.close();
dos.flush();

to read

DataInputStream dis = new DataInputStream(socket.getInputStream());
String propertiesText = dis.readUTF();
Properties properties = new Properties();
properties.load(new StringReader(propertiesText));
long lengthRemaining = dis.readLong();
FileOutputStream fos = new FileOutputStream(outFilename);
int len;
while(lengthRemaining > 0 
   && (len = dis.read(bytes,0, (int) Math.min(bytes.length, lengthRemaining))) > 0) {
      fos.write(bytes, 0, len);
      lengthRemaining -= len;
}
fos.close();
几度春秋 2024-10-25 02:16:52

您可以围绕众所周知的协议(如 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.

说好的呢 2024-10-25 02:16:52

我建议为此使用 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!

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