Results 1 to 4 of 4
  1. #1
    KoolCancer is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default 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");
    }
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    KoolCancer is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    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!!!!
    Attached Thumbnails Attached Thumbnails how to send a file from server to client and client saves the file?-tcpfile.jpg  

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 07:15 PM
  2. send file via client - server model
    By spasavvas in forum Networking
    Replies: 15
    Last Post: 08-13-2010, 12:45 PM
  3. How to open a file located in server from client side
    By Malathi in forum Web Frameworks
    Replies: 4
    Last Post: 04-20-2009, 10:10 PM
  4. Replies: 1
    Last Post: 02-10-2009, 10:16 AM
  5. Replies: 13
    Last Post: 04-28-2008, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •