aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes File transferring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "File transferring" Watch "File transferring" New topic
Author

File transferring

ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
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>
Les Dsouza
Greenhorn

Joined: Jan 29, 2002
Posts: 27
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 ]
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
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?
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
why not just use Ftp to send the file back and forth?


Sun Certified Programmer on the Java 2 Platform
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
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.
SJay
Greenhorn

Joined: Aug 15, 2002
Posts: 2
can I get a pointer or samples of FTP
Its Just Me
Greenhorn

Joined: Jan 10, 2002
Posts: 21
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...
Its Just Me
Greenhorn

Joined: Jan 10, 2002
Posts: 21
here's a link to a good FTP implementation:
http://www.enterprisedt.com/downloads/ftp.html
I've used it and it works pretty good...
Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
another FTP implementation ... also support SFTP (Secure FTP)
http://www.jscape.com/sftp/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File transferring