Results 1 to 4 of 4
- 07-28-2009, 05:28 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
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");
}
}
}
}
- 07-28-2009, 11:01 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You want to download the file from the server to client machine? Can you explain a bit you want. And what you have tried, please be specific.
- 07-28-2009, 11:27 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
i need to create 2 programs a client which requests for a file from the server and later when file is received, will save it and the other program- server will need to search for the requested file and if found send it to the client.
the 2 programs that i have written was supposed to do it BUT i have been only able to display the content of the file on both programs only. when it comes to writing to the file, i am not able to and the client programs remains "still" as if waiting for an event!!!!
- 07-29-2009, 04:52 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I've not go trough your code completely. Once you receive the content of the file to the other party, why you cannot write them to a file on that side? You have to pass the content from one side to another through a buffer. So where you stuck with?
Similar Threads
-
To transfer a file from client to server
By phani in forum NetworkingReplies: 4Last Post: 10-12-2010, 06:15 PM -
send file via client - server model
By spasavvas in forum NetworkingReplies: 15Last Post: 08-13-2010, 11:45 AM -
How to open a file located in server from client side
By Malathi in forum Web FrameworksReplies: 4Last Post: 04-20-2009, 09:10 PM -
Sending a .Doc file from client to a server through socket
By amita_k29 in forum NetworkingReplies: 1Last Post: 02-10-2009, 09:16 AM -
how to get a file from the server and save to disk of the client pc
By ralphus in forum New To JavaReplies: 13Last Post: 04-28-2008, 09:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks