Transfer File from Client to Server Using Java Socket Programming in Localhost

Transfer File from Client to Server Using Java Socket Programming in Localhost

BASIC

Java Socket is a java class which have features to share texts, files, mails and any types of data between two different PC (connected by cable) or in localhost. In this tutorial I have shown how to transfer file using Java Socket in your localhost. If you want to share file between two different PC then put the IP Address of the server PC to the client PC. For localhost use the IP Address of localhost or simply write “localhost” instead of IP Address.

IP Address of localhost is: “127.0.0.1”

DESCRIPTION

We have to write a java code that will share a file from client to server. First of all the client will send the name of the file. Then client will send the size of the file. The the client will send the file. The server will receive the file and will set the name what is received as name from client. Then server will check whether size of the sent file and received file is same or not. If size is same then server will print file is verified or if not server will print the file is corrupted. Server will print the received byte also. Below is the code.

Server.java

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		ServerSocket servsock = new ServerSocket(3456);
		Socket sock = servsock.accept();
		Scanner in = new Scanner(sock.getInputStream());
		InputStream is = sock.getInputStream();
		PrintWriter pr = new PrintWriter(sock.getOutputStream(), true);
		String FileName = in.nextLine();
		int FileSize = in.nextInt();
		FileOutputStream fos = new FileOutputStream(FileName);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		byte[] filebyte = new byte[FileSize];
		
		int file = is.read(filebyte, 0, filebyte.length);
		bos.write(filebyte, 0, file);
		
		System.out.println("Incoming File: " + FileName);
		System.out.println("Size: " + FileSize + "Byte");
		if(FileSize == file)System.out.println("File is verified");
		else System.out.println("File is corrupted. File Recieved " + file + " Byte");
		pr.println("File Recieved SUccessfully.");
		bos.close();
		sock.close();
	}
}

Client.java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket sock = new Socket("localhost", 3456);
		String FileName = "Manik.txt";
		File MyFile = new File(FileName);
		int FileSize = (int) MyFile.length();
		OutputStream os =sock.getOutputStream();
		PrintWriter pr = new PrintWriter(sock.getOutputStream(), true);
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(MyFile));
		Scanner in = new Scanner(sock.getInputStream());
		
		pr.println(FileName);
		pr.println(FileSize);
		byte[] filebyte = new byte[FileSize];
		bis.read(filebyte, 0, filebyte.length);
		os.write(filebyte, 0, filebyte.length);
		System.out.println(in.nextLine());
		os.flush();
		sock.close();
	}
}

You can view my video of this tutorial. Thank you for reading this tutorial. If you have anything to ask please comment. If this tutorial is helpful for you don’t forget to like comment and share with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *