简体   繁体   English

将文件存储在对象内

[英]Store a file inside an object

I have a Java client/server desktop application, where the communication between client and server is based on Sockets, and the messages exchanged between client and server are serialized objects (message objects, that incapsulate requests and responses). 我有一个Java客户机/服务器桌面应用程序,其中客户机与服务器之间的通信基于套接字,并且客户机与服务器之间交换的消息是序列化的对象(封装请求和响应的消息对象)。

Now I need to make the client able to upload a file from the local computer to the server, but I can't send the File through the buffer, since the Buffer is already used for exchanging the message objects. 现在,我需要使客户端能够从本地计算机上载文件到服务器,但是由于缓冲区已经用于交换消息对象,所以我无法通过缓冲区发送文件。

Should i open another stream to send the file, or is there any better way to upload a file for my situation? 我应该打开另一个流来发送文件,还是有其他更好的方式上传文件?

You really only have two options: 您实际上只有两种选择:

  1. Open another connection dedicated to the file upload and send it through that. 打开另一个专用于文件上传的连接,然后通过该连接进行发送。
  2. Make a message object representing bits of a file being uploaded, and send the file in chunks via these message objects. 创建一个表示要上载文件的位的消息对象,然后通过这些消息对象以块的形式发送文件。

The former seems simpler & cleaner to me, requiring less overhead and less complicated code. 在我看来,前者看起来更简单,更干净,所需的开销更少,代码也更简单。

I need to make the client able to upload a file from the local computer to the server

- Open a Solely Dedicated Connection to the Server for File uploading. -打开到服务器的Solely Dedicated Connection以上传文件。

- Use File Transfer Protocol to ease your work, and moreover its quite easy and reliable to use the Apache's common lib for File uploading and downloading .... -使用File Transfer Protocol可以简化您的工作,此外,使用Apache's common lib进行文件上载和下载也相当容易和可靠。

See this link: 看到这个链接:

http://commons.apache.org/net/ http://commons.apache.org/net/

您可以保留解决方案,并将文件内容作为对象(例如作为字符串)传递-如果内容包含麻烦的字符,请使用内容的Base64编码(或类似格式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM