How to Transfer Files Between Computers Using Java?

  • Context: Java 
  • Thread starter Thread starter ramollari
  • Start date Start date
  • Tags Tags
    Java Networking
Click For Summary

Discussion Overview

The discussion focuses on methods for transferring files between computers using the Java programming language. Participants explore various approaches, including networking classes, serialization, and potential protocols, while considering different environments such as LAN and the Internet.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant inquires about transferring files using Java and questions whether serialization is necessary.
  • Another participant suggests that if the transfer is over a LAN, serialization may not be needed, and recommends using ObjectInputStream and ObjectOutputStream.
  • It is noted that Java objects must be serialized to leave their virtual machine, although this may not need to be invoked explicitly.
  • A detailed method is proposed involving ServerSocket and Socket for file transfer, along with the need for a protocol to manage file names and commands.
  • One participant expresses a desire to avoid the complexity of FTP and suggests using ObjectOutputStream and ObjectInputStream for file object transfer.
  • Another participant emphasizes the necessity of networking classes for file transfer and mentions the importance of passing file names between computers.
  • There is a suggestion that if the transfer is on a LAN and network drives can be mounted, writing directly to a directory could be simpler.
  • One participant proposes defining a simpler protocol than FTP, while acknowledging the points made by others.
  • RMI is mentioned as an alternative for file transfer, but some participants argue it may not be suitable for this purpose.
  • Links to external code examples are shared as potential starting points for implementing file transfer.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of serialization, the complexity of protocols, and the suitability of RMI for file transfer. No consensus is reached on a single method or approach.

Contextual Notes

Participants highlight the need for a protocol to manage file transfers, the potential complexity of implementing such a system, and the importance of networking classes. There are also discussions about the limitations of certain methods like RMI for file transfer.

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..
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
86
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
4K