File Transfer via LAN using JAVA
Step 1- Create java source file named "fileSender.java"
import java.net.*;
import java.io.*;
class fileSender
{
public static void main(String arg[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in); // For Keyboard Input
DatagramSocket dgsocket=new DatagramSocket();
DatagramPacket datapack;
InetAddress destination;
System.out.print("Enter Machine IP Address / Name : "); // IP Address of Host (Receiver)
String ipadd=dis.readLine();
destination=InetAddress.getByName(ipadd);
System.out.print("Enter file name or path to be send : ");
String filepath=dis.readLine();
String filename=filepath.substring(filepath.lastIndexOf('\\')+1,filepath.length());
byte[]dt=filename.getBytes();
datapack=new DatagramPacket(dt,dt.length,destination,1212);
dgsocket.send(datapack); // Send File Name
File file=new File(filepath);
FileInputStream fis=new FileInputStream(file);
int filesize=fis.available();
byte[] data=new byte[filesize];
fis.read(data);
datapack=new DatagramPacket(data,data.length,destination,1212);
dgsocket.send(datapack); // Send Contents of file
System.out.println("\nFile Transfer Successful !");
dgsocket.close();
}
}
Step 2- Create java source file named "fileReceiver.java"
Machine 2
Now ! Enjoy........
import java.net.*;
import java.io.*;
class fileSender
{
public static void main(String arg[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in); // For Keyboard Input
DatagramSocket dgsocket=new DatagramSocket();
DatagramPacket datapack;
InetAddress destination;
System.out.print("Enter Machine IP Address / Name : "); // IP Address of Host (Receiver)
String ipadd=dis.readLine();
destination=InetAddress.getByName(ipadd);
System.out.print("Enter file name or path to be send : ");
String filepath=dis.readLine();
String filename=filepath.substring(filepath.lastIndexOf('\\')+1,filepath.length());
byte[]dt=filename.getBytes();
datapack=new DatagramPacket(dt,dt.length,destination,1212);
dgsocket.send(datapack); // Send File Name
File file=new File(filepath);
FileInputStream fis=new FileInputStream(file);
int filesize=fis.available();
byte[] data=new byte[filesize];
fis.read(data);
datapack=new DatagramPacket(data,data.length,destination,1212);
dgsocket.send(datapack); // Send Contents of file
System.out.println("\nFile Transfer Successful !");
dgsocket.close();
}
}
Step 2- Create java source file named "fileReceiver.java"
import java.net.*;
import java.io.*;
class fileReceiver
{
public static void main(String arg[])throws IOException
{
byte data[]=new byte[131072];
DataInputStream dis=new DataInputStream(System.in);
DatagramSocket dgsocket=new DatagramSocket(1212);
DatagramPacket rcvpack;
System.out.print("\nEnter Directory path and name where you want to save :");
String dirpath=dis.readLine();
System.out.println("\n\n\tWaiting for receiving file.........");
rcvpack=new DatagramPacket(data,data.length);
/************************* Getting file name*********************/
dgsocket.receive(rcvpack);
String filedata=new String(rcvpack.getData());
filedata=filedata.substring(0,rcvpack.getLength());
dirpath=dirpath+"\\"+filedata;
/*************************Getting file data *********************/
dgsocket.receive(rcvpack);
filedata=new String(rcvpack.getData());
filedata=filedata.substring(0,rcvpack.getLength());
/************************* Saving Content of file *****************/
FileOutputStream fout=new FileOutputStream(dirpath);
fout.write(filedata.getBytes());
System.out.println("\nFile received and Saved !");
}
}
Step 3: Compile & Run
Machine 1
Machine 2
Now ! Enjoy........
Nice.. Mr Sabab !!
ReplyDeleteWell Worked...
Excellent!!!!
ReplyDeletei cant sent or recive any thing help
ReplyDeletePlease run both program (First filereceiver then filesender) in separate command window simultaneously.
ReplyDeleteNice coding skill :)
ReplyDelete