• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

File transferring

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not just use Ftp to send the file back and forth?
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can I get a pointer or samples of FTP
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another FTP implementation ... also support SFTP (Secure FTP)
http://www.jscape.com/sftp/
 
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic