简体   繁体   中英

File not launching in Client Side Java (Server/Client) App no Web

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();

I solve it my self the answer is to use the code below:

replace: Desktop.getDesktop().open(file);

with: Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM