通过套接字将 Java 文件对象发送到服务器

发布于 2024-10-30 11:49:05 字数 273 浏览 6 评论 0原文

您好,我正在尝试通过套接字将 Java 文件对象发送到服务器,然后服务器将其存储在数据库中。目前我已经创建了一个 FileBean,其中存储 File 对象。然后,我使用 ObjectOutputStream 将 FileBean writeObject() 到服务器。然而,似乎 File 对象只包含对实际数据的引用,因此服务器无法实际获取数据。

有没有办法使用对象输出流之类的东西序列化要通过套接字发送的文件对象?或者这是否需要将文件写入缓冲区并通过套接字发送?

任何帮助和代码示例都是值得赞赏的

Hello I am trying to send a Java File Object over a socket to a server which will then store it in a database. Currently I have created a FileBean which stores the File object in it. I then use an ObjectOutputStream to writeObject() the FileBean to the Server. However, it seems as though the File object only contains a reference to the actual data, so the Server fails to actually get the data.

Is there a way to serialize the File object to be sent over the socket using something like an objectoutput stream? or does this require to writing of the file into a buffer and sending that across the socket?

any help and code examples is apprecaited

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

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

发布评论

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

评论(1

梦行七里 2024-11-06 11:49:05

文件就是路径,仅此而已。对于另一台机器上的服务器来说完全没有价值。

这是文件的所有状态(取自 java.io.File 的 Java 1.6 源代码):

/**
 * This abstract pathname's normalized pathname string.  A normalized
 * pathname string uses the default name-separator character and does not
 * contain any duplicate or redundant separators.
 *
 * @serial
 */
private String path;

/**
 * The length of this abstract pathname's prefix, or zero if it has no
 * prefix.
 */
private transient int prefixLength;

您必须读取文件的内容(可能作为字节数组)并将它们发送到服务器。

A file is a path, not much more. And totally worthless to a server on a different machine.

This is all the state a file has (taken from the Java 1.6 source of java.io.File):

/**
 * This abstract pathname's normalized pathname string.  A normalized
 * pathname string uses the default name-separator character and does not
 * contain any duplicate or redundant separators.
 *
 * @serial
 */
private String path;

/**
 * The length of this abstract pathname's prefix, or zero if it has no
 * prefix.
 */
private transient int prefixLength;

You will have to read the file's contents (probably as a byte array) and send them to the server.

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