0

I am currently working on an application and I wrote it with Java. It is downloading some media files to local computer and open it with a Java method called Desktop.getDesktop().open(file); It is working good on windows but it is not working on debian. Here is used download from url method:

            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;

Then I want to open that file like that:

File f = new File(url);
Desktop.getDesktop().open(f);

And the error says;

error here

Any suggestion ? Thanks

Ozan
  • 1,191
  • 2
  • 16
  • 31
  • You can make your download code considerably shorter by using the [Files.copy](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-) method. – VGR Sep 06 '16 at 18:04

1 Answers1

0

I solved that with using this , so when I open file I am using xdg-open..

Community
  • 1
  • 1
Ozan
  • 1,191
  • 2
  • 16
  • 31