开发者

Send Java File object to a Server over Sockets

开发者 https://www.devze.com 2023-02-22 06:17 出处:网络
Hello I am trying to send a Java File Object over a socket to a server which will then store it in 开发者_JAVA百科a database. Currently I have created a FileBean which stores the File object in it. I

Hello I am trying to send a Java File Object over a socket to a server which will then store it in 开发者_JAVA百科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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号