how to send a file from server to client and client saves the file?
Hi all,
i am to write 2 programs:
client: request file from server
server: searches for file in specified directory; if found sends same to client
client: when file is received, saves it.
i have written the programs and have been able to display the file content on both server and clients but saving the file is a real problem for me.
can anyone help me please?
CLIENT CODE:
import java.io.*;
import java.net.*;
public class TCPFileClient
{ public static void main(String args[]) throws Exception
{ String file = "";
String serverfile="";
while(!(file.equals("exit"))) {
System.out.print("Enter File name:");
BufferedReader readerfromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1", 12);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
file = readerfromUser.readLine();
outToServer.writeBytes(file + '\n');
BufferedReader readerFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
FileWriter fw= new FileWriter("TCPFileClientcopy1.txt");
while(!(serverfile.equals(null)))
{ System.out.println("FromServer " + serverfile);
serverfile = readerFromServer.readLine();
fw.write(serverfile);
}
fw.close();
clientSocket.close();
}
}
}
SERVER CODE
import java.io.*;
import java.net.*;
import java.io.File;
import java.util.Scanner;
public class TCPFileServer
{
public static void main(String args[]) throws Exception
{ //declaring variables
String clientfile;
String reply = "";
//declaring a ServerSocket obj intialising with a socket
ServerSocket serverSocket = new ServerSocket(12);
while(!(reply.equals("exit")))
{ System.out.print("waiting for request:");
//declaring a socket obj and assigning the values
Socket connectionSocket = serverSocket.accept();
//Declaring a variable for capturing the message from Client
//the bufferedReader will buffer an InputStream Obj that will read from the socket
BufferedReader readerFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream( )));
//Declaring an output stream for replying to client
DataOutputStream writeToClient = new DataOutputStream(connectionSocket.getOutputStream( ));
//BufferedOutputStream writeToClient = new BufferedOutputStream(connectionSocket.getOutputStr eam());
clientfile = readerFromClient.readLine();
System.out.println("File requested is: "+ clientfile);
File directory; // File object referring to the directory.
String[] files; // Array of file names in the directory.
directory = new File("C:\\Documents and Settings\\Rizwan\\Desktop\\assignments\\Java Progs");
System.out.println("search for files in " + directory);
if (directory.isDirectory() == false)
{
if (directory.exists() == false)
System.out.println("There is no such directory!");
else
System.out.println("That file is not a directory.");
}
else
{
files = directory.list();
//System.out.println("Files in directory \"" + directory + "\":");
//for (int i = 0; i < files.length; i++) {
//System.out.println(" " + files[i]);
File checkfile = new File(clientfile);
boolean bo = checkfile.exists();
//System.out.println("Does File/Dir " + file1 + " exist? (" + b + ")\n");
if (bo == true)
{
System.out.println("That file exists");
FileReader fr = new FileReader(checkfile);
BufferedReader br = new BufferedReader (fr);
String line ="" ;
while (line != null)
{ System.out.println(line);
line = br.readLine();
writeToClient.writeBytes(line + "\n");
//System.out.println("message replied:" + reply);
}
br.close();
/*BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(new File("TCPFileServer.txt")));
byte [] b = new byte[4096];
int bytesRead;
while ((bytesRead = buffIn.read(b)) != -1)
{ writeToClient.write(b, 0, bytesRead);
*/
/*System.out.print("mesage reply:");
BufferedReader replyfromserver = new BufferedReader(new InputStreamReader(System.in));
reply = replyfromserver.readLine();
writeToClient.writeBytes(reply + "\n");
System.out.println("message replied:" + reply);*/
}
//else
System.out.println("That file does not exist");
}
}
}
}