Search code examples
javadesktop-applicationnetbeans-platform

Java Desktop file download


While making a desktop application,I want it to be able to download a video file from server and save it in client's PC. How to do it?I have no idea. What tools to use?Please guide I am looking forward to some kind of integrated FTP like thing.I am not clear how it will work.


Solution

  • Where you wanna download the file? direct url or youtube like?

    Something like that:

    public class DownloadToFile
    {
        private URL from;
        private String to;
    
        public DownloadToFile(URL url, String path_to)
        {
            this.URL = url;
            this.to = path_to;
        }
    
        public DownloadToFile(String url, String path_to)
        {
            try
            {
                this.URL = new URL(url);
            }
            catch(MalformedURLException e)
            {
                e.printStackTrace();
            }
    
            this.to = path_to;
        }
    
        public void saveFile() throws IOException 
        {
            BufferedInputStream in = new BufferedInputStream(this.from.openStream());
            FileOutputStream out = new FileOutputStream(this.to);
                try 
                {
                    final byte data[] = new byte[1024];
                        int count;
                            while ((count = in.read(data, 0, 1024)) != -1)
                            {
                                out.write(data, 0, count);
                            }
                }
                finally
                {
                    if (in != null)
                    {
                        in.close();
                    }
            if (fout != null)
            {
                fout.close();
            }
        }
    }