This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File transferring

 
ryan headley
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Les Dsouza
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not just use Ftp to send the file back and forth?
 
ryan headley
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I get a pointer or samples of FTP
 
Its Just Me
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another FTP implementation ... also support SFTP (Secure FTP)
http://www.jscape.com/sftp/
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic