The file does not start in the Java client (server / client)

advertisements

Problem: My File not Launching which is requested from sever.

Okay I have written a server/client application but the problem is when i request for a file from the server it transfer over to the client but what I have notice is that I need to manually refresh the directory to get the file to be in the path or directory. So by saying that, I feel that this is why my code when request the file it doesn't launch.

My approach launching the file from the client after it had just been requested.

Here is my code below:

public static void receiveFile(String fileName) {
    try {
        int bytesRead;
        InputStream in = sock.getInputStream();

        DataInputStream clientData = new DataInputStream(in);

        fileName = clientData.readUTF();
        OutputStream output = new FileOutputStream((fileName));//need to state a repository
        long size = clientData.readLong();
        byte[] buffer = new byte[1024];
        while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
            output.write(buffer, 0, bytesRead);
            size -= bytesRead;
        }

        output.close();
        in.close();
        File file = new File(fileName);
        Desktop.getDesktop().open(file);

        //System.out.println("File "+fileName+" received from Server.");
    } catch (IOException ex) {
        //Logger.getLogger(CLIENTConnection.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace();
    }
}

Please can you look are tell me what you think I am doing wrong? Server code:


Try flush the stream before close.

output.flush();
output.close();