Who knows networking with Java

  • Thread starter ramollari
  • Start date
  • #1
437
1
Hi,
Does anyone know a way how to transfer a file from one computer to the other with Java language? Should I serialize it as an object?
 

Answers and Replies

  • #2
644
1
Are u working on lan or internet?
If its on lan , it is easy and no i dont think u need to serialize it. Use ObjectInputStream and ObjectOutputStream which does the job for u.

-- AI
 
  • #3
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
Any Java object must be serialized in order to leave the virtual machine in which it resides. But, as Tenali mentions, it's not something you invoke explicitly.
 
  • #4
chroot
Staff Emeritus
Science Advisor
Gold Member
10,226
34
So you want to copy a file from your disk, across the network to another computer, and put the file on its disk?

You'll need to use a ServerSocket on one computer to create a server which can be connected to by the other computer. The other computer will use a normal Socket.

You will have to use FileInputStream and FileOutputStream to read and write the files from the disk.

This is a potentially complicated program, because you'll have to develop some kind of a protocol in which the computers can tell each other the names of the files, etc. If you want to be able to transfer files in both directions, you'll have to work out a protocol in which one computer can tell the other what to do, etc.

You might want to consider implementing a subset of the File Transfer Protocol, http://www.w3.org/Protocols/rfc959/. If you do this, you'll be able to test and use your program with a large variety of FTP clients already available for free.

- Warren
 
  • #5
437
1
No Warren I don't intend to build something as complicated as the FTP. I just thought I could transfer file objects with the ObjectOutputStream and ObjectInputStream from one machine to the other. Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
 
  • #6
644
1
ramollari said:
Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
Err he didnt mean that.
For example when u have transferred a file to the other side, say for example the client would like to store it with the same name as on the server side , then u would have to pass the filename to the other side (we had to do this when we had implemented a small teaching tool at our college).

-- AI
 
  • #7
chroot
Staff Emeritus
Science Advisor
Gold Member
10,226
34
ramollari said:
No Warren I don't intend to build something as complicated as the FTP. I just thought I could transfer file objects with the ObjectOutputStream and ObjectInputStream from one machine to the other. Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
This is not correct. You will need to involve actual networking classes, like Socket and ServerSocket, to accomplish moving files across a network.

- Warren
 
  • #8
294
0
If it is on the LAN, and you can mount network drives, they its as easy as writing the file to the directory.

If it is over the Internet however, you will need to user network sockets, and you will need a simple protocol. It shouldn't be too bad, however.
 
  • #9
437
1
What I mean is that I can define a much simpler protocol than the FTP, whithout the 'get', 'put', 'list', etc. etc. commands. Anyway I get your points.
 
  • #10
294
0
Actually another alternative is to use RMI, but that would probably more complicated than its worth.
 
  • #11
437
1
No, RMI doesn't work for file transfer.
 
  • #13
294
0
lyapunov said:
Found some code that might be useful as a starting point:

http://forums.devshed.com/archive/t-8632/file-transfer-in-servlets
Servlets require a webserver(or a webserver-lite like Tomcat), which is probably more than he needs.

ramollari said:
No, RMI doesn't work for file transfer.
Why couldn't you invoke a remote method on the host that will write out to a local file? Im not saying its the easiest way or anything(you have to set up an RMI registry on the host for one), but I don't see any reason why its not possible..
 

Related Threads on Who knows networking with Java

  • Last Post
Replies
13
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
10
Views
3K
Top