Posted By:
21 Feb, 2014 10:01 am PST

Programs to transfer File contents from the Server to the Client

Develop a code that accepts the file name and checks if it exists or not. If the file exists on the server side, the server sends the contents of the file to the client.

//create a server that send file contents to client

import java.net.*;

import java.io.*;

public class SendFile {

       public static void main(String[] args) throws Exception {

              //create a server socket

              ServerSocket se=new ServerSocket(123);

              //let the server wait until the connection is accepted by client

              Socket q=se.accept();

              System.out.println("Connection established successfully");

              //to receive file name from client

              BufferedReader v=new BufferedReader(new InputStreamReader(q.getInputStream()));

              //to transfer file contents to client

              DataOutputStream dr=new DataOutputStream(q.getOutputStream());

              //read file name from client

              String g=v.readLine();

              FileReader f=null;

              BufferedReader ff=null;

              boolean b;

              //create file class object with file name

              File r=new File(g);

              //test if file exists or not

              if(r.exists())

                     b=true;

              else

                     b=false;

              //if file exists, send 'yes' to client else send 'no'

              if(b==true) dr.writeBytes("Yes"+ "\n");

              else  dr.writeBytes("No"+"\n");

              if(b==true)

              {

              //attach file to fileReader to read data

              f=new FileReader(g);

              //attach FileReader to BufferedReader

                     ff=new BufferedReader(f);

                     String qq;

              //read from BufferedReader and write to DataOutputStream

                     while((qq=ff.readLine())!=null)

                     {

                           dr.writeBytes(qq+"\n");

                     }

                     dr.close();

                     ff.close();

                     v.close();

                     se.close();

                     q.close();

                     f.close();

                                  }

       }

}

Write a client program to accept a file name from the keyboard and send that name to the server. The client receives the file contents from the server.

//to receive the file contents from the server

import java.net.*;

import java.io.*;

public class ReceiveFile {

       public static void main(String[] args) throws Exception{

              //create client Socket

              Socket s=new Socket("localhost",123);

       //accept file name from keyboard

              BufferedReader k=new BufferedReader(new InputStreamReader(System.in));

              System.out.print("Enter file name:  ");

String filename=k.readLine();

//send file name to server using DataOutputStream

DataOutputStream d=new DataOutputStream(s.getOutputStream());

d.writeBytes(filename +"\n");

//to read data sent from server

BufferedReader i=new BufferedReader(new InputStreamReader(s.getInputStream()));

String st;

//read first line from server into st

st=i.readLine();

//if file is found on server side, then send "Yes" else "No"

if(st.equals("Yes"))

{

       //read and display the file contents coming from Server

       while((st=i.readLine())!=null)

              System.out.println(st);

//close all connections

i.close();

d.close();k.close();

s.close();

}

       else

System.out.println("File not found");          

}

}

OUTPUT:

 



Description:

Java.net : Provides the classes for implementing networking applications.

Serversocket class: This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

Socket : This class implements client sockets .

 

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud