java文件放桌面怎么运行不了,不可能使用Java桌面在网络上启动文件吗?

Java 6解决方案:

public static void launchFile(File file) {

if (!Desktop.isDesktopSupported())

return;

Desktop dt = Desktop.getDesktop();

try {

dt.open(file);

} catch (IOException ex) {

// this is sometimes necessary with files on other servers ie

// \\xxx\xxx.xls

launchFile(file.getPath());

}

}

// this can launch both local and remote files

public static void launchFile(String filePath) {

if (filePath == null || filePath.trim().length() == 0)

return;

if (!Desktop.isDesktopSupported())

return;

Desktop dt = Desktop.getDesktop();

try {

dt.browse(getFileURI(filePath));

} catch (Exception ex) {

ex.printStackTrace();

}

}

// generate uri according to the filePath

private static URI getFileURI(String filePath) {

URI uri = null;

filePath = filePath.trim();

if (filePath.indexOf("http") == 0 || filePath.indexOf("\\") == 0) {

if (filePath.indexOf("\\") == 0){

filePath = "file:" + filePath;

filePath = filePath.replaceAll("#", "%23");

}

try {

filePath = filePath.replaceAll(" ", "%20");

URL url = new URL(filePath);

uri = url.toURI();

} catch (MalformedURLException ex) {

ex.printStackTrace();

} catch (URISyntaxException ex) {

ex.printStackTrace();

}

} else {

File file = new File(filePath);

uri = file.toURI();

}

return uri;

}

这个答案在bug报告中,但我已经编辑了它,以便在有哈希值时修复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值