java如何打开一个带有IP地址的文件,不要下载到本地..
看了半天api,还是调不通,请高手帮忙,谢谢!!~~代码如下:importjava.awt.Desktop;importjava.io.File;importjava.i...
看了半天api,还是调不通,请高手帮忙,谢谢!!~~
代码如下:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class OpenFile {
private static File file;
private static URI uri;
public static void main(String[] args) throws URISyntaxException{
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
String str = "file:\\\\192.168.23.64\\folder1\\folder2 folder3\\test.txt ";
System.out.println(str);
File file = new File(str);
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
而且用下面的代码,也能打来相应的文件
import java.io.IOException;
public class Openfile {
public static void main(String[] args) {
String addr = "file:\\\\192.168.23.64\\folder1\\folder2\\test.txt ";
try {
Runtime.getRuntime().exec("cmd /c start " +addr + "\" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
也就是说这个路径也没问题
只不过用 cmd 这个打开的文件,完全路径名中不能带有空格。。。着实郁闷 展开
代码如下:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class OpenFile {
private static File file;
private static URI uri;
public static void main(String[] args) throws URISyntaxException{
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
String str = "file:\\\\192.168.23.64\\folder1\\folder2 folder3\\test.txt ";
System.out.println(str);
File file = new File(str);
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
而且用下面的代码,也能打来相应的文件
import java.io.IOException;
public class Openfile {
public static void main(String[] args) {
String addr = "file:\\\\192.168.23.64\\folder1\\folder2\\test.txt ";
try {
Runtime.getRuntime().exec("cmd /c start " +addr + "\" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
也就是说这个路径也没问题
只不过用 cmd 这个打开的文件,完全路径名中不能带有空格。。。着实郁闷 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询