Java Runtime.getRuntime().exec(),如何从网站链接执行文件?

Java Runtime.getRuntime().exec(),如何从网站链接执行文件?,java,runtime,execute,calc,Java,Runtime,Execute,Calc,到目前为止,我的这一行运行良好,它在我的计算机上执行calc.exe: Runtime.getRuntime().exec("calc.exe"); 但是如何从网站链接下载并执行文件呢?比如说 我在网上找到了这段代码,但它不起作用: Runtime.getRuntime().exec("bitsadmin /transfer myjob /download /priority high http://website.com/calc.exe c:\\calc.exe &start ca

到目前为止,我的这一行运行良好,它在我的计算机上执行calc.exe:

Runtime.getRuntime().exec("calc.exe");
但是如何从网站链接下载并执行文件呢?比如说

我在网上找到了这段代码,但它不起作用:

Runtime.getRuntime().exec("bitsadmin /transfer myjob /download /priority high http://website.com/calc.exe c:\\calc.exe &start calc.exe");
您可以使用和/或将其保存在某个位置(例如,当前工作目录或临时目录),然后使用
运行时.getRuntime().exec()

执行它,并将其作为您可以执行此操作的起点:(使用)


@KarleeCorinne点击链接,上面写着下载文件,它有下载链接并将其保存为文件的示例代码。只需将
“C:/mura-latest.zip”
替换为您要将其另存为的文件即可。是的,但它似乎包含许多不必要的代码。另外,如果我将其放在目录C:/Windows/System32/中,是否需要通过执行C://Windows///System32//来转义字符?然后对于运行时,我可以只执行runtime.getRuntime().exec(“file.exe”)@你可以跳过所有的打印输出和计时器,但你还需要其他一切。您可以使用当前目录,如:
newfile(“File.exe”)
然后
Runtime.getRuntime().exec(“File.exe”)
当前目录是什么?@KarleeCorinne它是您运行java程序的地方
public static void main(String... args) throws IOException {
    System.out.println("Connecting...");
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://website.com/calc.exe");
    HttpResponse response = client.execute(get);

    InputStream input = null;
    OutputStream output = null;
    byte[] buffer = new byte[1024];

    try {
        System.out.println("Downloading file...");
        input = response.getEntity().getContent();
        output = new FileOutputStream("c:\\calc.exe");
        for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
        System.out.println("File successfully downloaded!");
        Runtime.getRuntime().exec("c:\\calc.exe");

    } finally {
        if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
}