简体   繁体   English

文件未在客户端Java(服务器/客户端)App No Web中启动

[英]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); 替换为:Desktop.getDesktop()。open(file);

with: Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file); 使用:Runtime.getRuntime()。exec(“ rundll32 url.dll,FileProtocolHandler” + file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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