fara
fara

Reputation: 49

sending a file to client in java

i'm new to java. I'm trying to create a simple java file server from where the clients can request for a file and download it. basically when a client requests for a file it will simply will be written from server's folder to client folder. When i run my code it does not show any error but the file that client requested is not written to it's folder either.

my client side code:

public void download(Socket s) throws Exception {
     DataInputStream din=new DataInputStream(s.getInputStream());
     DataOutputStream dout=new DataOutputStream(s.getOutputStream());
     BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
     BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
     PrintWriter w = new PrintWriter(s.getOutputStream(), true);
    System.out.print("Enter File Name :");
    String request = con.readLine();
    w.println(request);
    String msg = r.readLine();
    if (msg.startsWith("ERROR")) {
        System.out.println("File not found on Server ...");
        return;
    } else if (msg.startsWith("FOUND")) {
        System.out.println("Receiving File ...");
        File f = new File(request);
        if (f.exists()) {
            String Option;
            System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
            Option = con.readLine();
            if (Option == "N") {
                dout.flush();
                return;
            }
        }
        FileOutputStream fileout = new FileOutputStream(f);
        int ch;
        String temp;
        do {
            temp = din.readLine();
            ch = Integer.parseInt(temp);
            if (ch != -1) {
                fileout.write(ch);
            }
        } while (ch != -1);
        fileout.close();
        System.out.println(din.readLine());
    }
}

The server side:

public class Fileagent extends Thread {

    Socket client;
    DataInputStream din;
    DataOutputStream dout;
    ServerSocket soc;
    PrintWriter w;
    BufferedReader r;

    public Fileagent(Socket soc) {
        try {
            client = soc;
            din = new DataInputStream(client.getInputStream());
            dout = new DataOutputStream(client.getOutputStream());
            w = new PrintWriter(client.getOutputStream(), true);
            r = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("FTP Client Connected ...");
            start();

        } catch (Exception ex) {
        }
    }

    public void upload() throws Exception {


        w.println("SEnding.....");
        String file = r.readLine();
        File f = new File(file);
        if (!f.exists()) {
            w.println("ERROR");
            return;
        } else {
            w.println("FOUND");
            FileInputStream fin = new FileInputStream(f);
            int ch;
            do {
                ch = fin.read();
                w.println(String.valueOf(ch));
            } while (ch != -1);
            fin.close();
        }

I'm trying to send simple text files but the files is not being send to clients.

Thanks in advance.

Upvotes: 0

Views: 2426

Answers (2)

corsiKa
corsiKa

Reputation: 82599

It looks like your problem stems from this

String request=con.readLine();

You're always reading from this con object. But you're passing in a Socket s to the method.

There are other problems, such as what Gray mentioned, and also that you're writing each character on its own line, but those are just messed up formatting; they shouldn't prevent you from getting a file at all...

Upvotes: 1

Gray
Gray

Reputation: 116938

I suspect the problem is that you are not flushing your PrintWriter after sending the request from the client to the server:

w.println(request);
w.flush();

You seem to be using a PrintWriter on the server side as well. Make sure to call w.flush() or w.close() when you are done sending stuff over.

Also, I assume you realize that this is an extremely inefficient way to send the file over.

Upvotes: 1

Related Questions