坚持下载jar文件[java]

时间:2018-08-23 14:11:25

标签: java jar

我正在使用下面的代码下载jar文件,但是它无法正常工作。它立即将“完成”,我认为这表明它可以找到链接,但无法下载。

它也运行if(readFiletoDownload!= null),这很奇怪,因为它创建了一个0 kb(178字节)的jar。

@SuppressWarnings("serial")
    public class tester extends JFrame {


        private static String downloadUrl = "http://dl.dropboxusercontent.com/s/11794utl5du0w6m/a.jar?dl=1r";
        private static String fileName = "SomeJar.jar";
        private static String serverName = "Pyxis";
        private static String saveDirectory = System.getProperty("user.home")+"/Desktop/";

        public static URL url;
        private static JProgressBar pbar;
        Thread t = null;

        public tester() {
            super("tester");
            File file = new File(saveDirectory + fileName);

            try {
                url = new URL(downloadUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            setSize(543, 391);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setLocationRelativeTo(null);
            setUndecorated(true);

            setLayout(null);
            pbar = new JProgressBar();
            pbar.setMinimum(0);
            pbar.setMaximum(100);
            pbar.setStringPainted(true);
            pbar.setForeground(Color.LIGHT_GRAY);
            add(pbar);
            pbar.setPreferredSize(new Dimension(310, 30));
            pbar.setBounds(70, 320, 404, 20);



            Thread t = new Thread() {

                public void run() {

                    OutputStream dest = null;
                    URLConnection download;
                    InputStream readFileToDownload = null;
                    try {
                        dest = new BufferedOutputStream(new FileOutputStream(saveDirectory + fileName)); 
                        download = url.openConnection();
                        readFileToDownload = download.getInputStream();
                        byte[] data = new byte[1024];
                        int numRead;
                        long numWritten = 0;
                        int length = download.getContentLength();
                        while ((numRead = readFileToDownload.read(data)) != -1) {
                            dest.write(data, 0, numRead);
                            numWritten += numRead;
                            int percent = (int)(((double)numWritten / (double)length) * 100D);
                            pbar.setValue(percent);
                            pbar.setString(""+(percent < 99 ? "Downloading "+serverName+" - "+percent+"%" : "Complete")+"");
                        }
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    } finally {
                        try {
                            if (readFileToDownload != null)
                                readFileToDownload.close();
                            if (dest != null)
                                dest.close();
                            Thread.sleep(1000L);
                        } catch (IOException | InterruptedException ioe) {

                        }
                    }
                }
            };
            setVisible(true);
            t.start();
        }

0 个答案:

没有答案