Java How to Transfer Files Between Computers Using Java?

  • Thread starter Thread starter ramollari
  • Start date Start date
  • Tags Tags
    Java Networking
AI Thread Summary
Transferring files between computers using Java can be achieved through various methods, depending on whether the connection is over a local area network (LAN) or the internet. For LAN transfers, using `ObjectInputStream` and `ObjectOutputStream` is suggested, but it is emphasized that actual networking classes like `Socket` and `ServerSocket` are necessary for file transfers across networks. A simple protocol is recommended for communication between the two computers, particularly to handle file naming and transfer commands. While some participants mention alternatives like Remote Method Invocation (RMI) and File Transfer Protocol (FTP), it is noted that RMI is not suitable for file transfers and that implementing FTP may be overly complex for simpler needs. For internet transfers, establishing a basic protocol without the complexities of FTP is advised. Additionally, using mounted network drives can simplify file writing on a LAN. Overall, the discussion highlights the importance of understanding networking fundamentals in Java for effective file transfer implementation.
ramollari
Messages
433
Reaction score
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?
 
Technology news on Phys.org
Are u working on lan or internet?
If its on lan , it is easy and no i don't think u need to serialize it. Use ObjectInputStream and ObjectOutputStream which does the job for u.

-- AI
 
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.
 
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
 
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.
 
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
 
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
 
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.
 
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
Actually another alternative is to use RMI, but that would probably more complicated than its worth.
 
  • #11
No, RMI doesn't work for file transfer.
 
  • #12
Found some code that might be useful as a starting point:

http://forums.devshed.com/archive/t-8632/file-transfer-in-servlets
 
  • #13
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? I am 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..
 
Back
Top