If I want to pass a file from one machine to another, how to I do so. I've read some thing on here but don't see exaclty how it happens. Say I want to send a file, test.txt to a machine: If I have a ServerSocket on my receiving machine then tie that to a Socket, get the InputStream, create an ObjectInputStream from the InputStream and then perform readObject() the file should come on over, but 1) How do I make the readObject back into what it was before (test.txt) 2) How do I know when the file is done?
This is my reciever
This is my sender:
Mind you this is very crude, but I am trying the simplest format first before I make it harder on myself.
Ryan Headley<br /><a href="http://www.sudovi.com" target="_blank" rel="nofollow">http://www.sudovi.com</a>
A File object only represents a file - it does not contain the actual file contents. So sending a file object across to the other machine is not going to help you. The javadoc says a File is "An abstract representation of file and directory pathnames"
What you need to do is read the file at the sender side using a FileInputStream and write these bytes to the socket's output stream. At the receiver end you can read the bytes with the sockets inputstream, and write them out to an fileoutputstream. An inputstream's read method will return -1 at the end of file. [ February 07, 2002: Message edited by: Les Dsouza ]
Joined: Jun 28, 2000
So basically there's now way to send the file in a "clump" so to speak? What if the file is a binary executable? Do I read it in the same way and then just make sure it gets the same name when copied? If I understand correctly its kind of like taking it apart and putting it back together on the other side...right? Will this work for all files or will the different file types need different streams?
why not just use Ftp to send the file back and forth?
Sun Certified Programmer on the Java 2 Platform
Joined: Jun 28, 2000
Well, that would be my first choice, but this is for my learning experience AND if I get it to work well enough...I will incorporate into my instant Java based instant messenger that I wrote for the company where I work.
Tom Ben wrote:
"why not just use Ftp to send the file back and forth?"
basically, FTP does exactly the same: take it apart, ship the data through the socket to the other end and build it again at the other side. Block of bytes by block of bytes... Only, if you use some third party classes to do this, you don't notice this.
(for moving a file from one location on your hard drive to another, you have to do exactly the same also, there's no method for this)
by the way, my comment is based on using the JDK 1.1, maybe in the later versions such utilities have been incorperated, I don't know...