Sending files between client and server...what is the best way?
I was wondering if anyone has had any experience with sending files from client to server. I am currently experimenting with sending files using the object streams by reading the file into a byte[] and then sending it through the object stream. this seems to work fine, but i wasn't sure if this was the best way to do it. I didn' t know if this was a least efficient way to do it, or if i would run into any pitfalls or major issues.
I did some googling but wasn't able to find much in reference to sending files using java. Any input or ideas from experience would be greatly appreciated.
thanks
"Best" != "most effiecient."
What's "best" for your particular situation depends on many things--size of the files, nature of their content, how often you'll be sending, available bandwidth, latency and throughput requriements, how quickly and correctly you're able to develop a solution using a given approach.
If reading and serializing a byte array works for you, then that's good enough. But can you be sure the files will always be small enough to fit into memory all at once?
Are they text files? If so, a more scalable approach would be to use a BufferedReader or Scanner to read a line at a time and then send each line after reading it. This also has the advantage that if the receiver uses a PrintWriter to write the lines out to a file on his end, you'll automatically get correct end-of-line conversion of the platforms are dissimilar.
If you want "efficient," you might get performance gains by compressing the files either up front or on the fly.
i am actually going to be sending numerous file types. I liked the ability to use the oos because i can create a simple object that can hold the file and information about the current user such as name and age, that can be stored in a database along with the file. it also has the ability to do whatever file is needed and allow it to be rebuilt again as long as the file extension is known.
also to help against running out of memory, if i am reading numerous files to be sent, i read one, then send it, then read the other..upon sending the serialized byte[], will that memory be freed up after it is sent? or is this something i should incorporate into my code?
Thanks for your help
> also to help against running out of memory, if i am
> reading numerous files to be sent, i read one, then
> send it, then read the other..upon sending the
> serialized byte[], will that memory be freed up after
> it is sent? or is this something i should incorporate
> into my code?
If you don't keep any references to those byte[]s (like adding each byte[] to a List, for instance) then their memory will be reclaimed if it's needed. The situation I was talking about was if a single file is too big to fit into memory. Just wanted to make sure you're aware of the possibility.