my dog learned polymorphism
The moose likes Sockets and Internet Protocols and the fly likes Transfering Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Transfering Files" Watch "Transfering Files" New topic

Transfering Files

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi All,
Can anyone tell how can i transfer file from 1 computer to another through java Code?
My problem is that client will provide me with a file path at his computer and i want to bring that file on my computer. How can i do that.
Please provide any code snipt or suggest any good Tutorial.Please reply soon. I'll be gratefull.
[ April 08, 2004: Message edited by: Ali Gohar ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9150

Please don't post the same question in multiple forums

[How To Ask Questions On JavaRanch]
Stanley George
Ranch Hand

Joined: Dec 09, 2000
Posts: 55
Hi Ali,
The following code works for me:
public void sendFile(File file, Socket socket) throws Exception {
FileInputStream fis = new FileInputStream(file);
DataOutputStream dos = new
DataInputStream dis = new DataInputStream(fis);
int b =;
while (b != -1) {
b =;
public void recvFile(File file, Socket socket) throws Exception {
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
DataInputStream dis = new DataInputStream(socket.getInputStream());
int b =;
while (b != -1) {
b =;
Stanley George
[ April 09, 2004: Message edited by: Stanley George ]
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
Another way is to use DATAGRAM Packet (UDP) protocol, but this method may stops the transmitting. But it is official accepted. look at a search for socket tutorials
I agree. Here's the link:
subject: Transfering Files
It's not a secret anymore!