0
我目前正在研究一个应用程序,并使用Java编写它。它将一些媒体文件下载到本地计算机,并使用名为Desktop.getDesktop().open(file);
的Java方法打开它。它在Windows上工作良好,但它不适用于debian。 这里被用来下载从以下网址方法:Debian上的Java应用程序打开并播放媒体错误
public String DownloadFromUrlAndGetPath(String DownloadUrl) {
String fileName="";
try {
URL url = new URL(DownloadUrl);
URLConnection ucon = url.openConnection();
String raw = ucon.getHeaderField("Content-Disposition");
// raw = "attachment; filename=abc.mp3"
if(raw != null && raw.indexOf("=") != -1) {
fileName = raw.split("=")[1]; //getting value after '='
fileName = fileName.replace("\"", "").trim();
} else {
return "";
}
File file = new File(Paths.get(System.getProperty("user.home"), "MyFolderToSaveFiles") +"/"+ fileName);
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
try {
baf.append((int)((byte)current));
continue;
}
catch (Exception var12_13) {
}
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
}
catch (IOException e) {
e.getMessage();
}
}
return Paths.get(System.getProperty("user.home"), "MyFolderToSaveFiles") +"/"+ fileName;
然后我想打开该文件这样的:
File f = new File(url);
Desktop.getDesktop().open(f);
和错误说;
什么建议吗?由于
你可以让你的下载代码,大大使用[Files.copy(HTTP短://文档.oracle.com/JavaSE的/ 8 /文档/ API/JAVA/NIO /文件/ Files.html#复制的java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption ...- ) 方法。 – VGR