开发者

File download from url on MAC OSX?

开发者 https://www.devze.com 2023-02-17 08:05 出处:网络
AM trying to download a file from the net using the url & urlconnection class in java using this code.

AM trying to download a file from the net using the url & urlconnection class in java using this code.

BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(destination);
            } catch (FileNotFoundException ex) {
            }
            try {
                URL url = null;
                try {
                    url = new URL("here is the url");
//                    
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                }
                URLConnection urlc = null;

                try {
                    urlc = url.openConnection();
                    System.out.println("Conneceted");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    bis = new BufferedInputStream(urlc.getInputStream());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                bos = new BufferedOutputStream(fos);
                //          System.out.println("absolute path="+destination.getAbsolutePath());

                int i;
                try {
                    while ((i = bis.read()) != -1) {
                        System.out.print((char) i);
                        bos.write(i);
                    }

                    JOptionPane.showMessageDialog(this, "downloadeded sucessfully");
                    this.dispose();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(this, "could not be diownloaded \n please try again");
                    ex.printStackTrace();
                }
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
                if (bos != null) {
                    try {

                        bos.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }

When i run this code on my windows system it runs perfectly but when i take my code to mac os it gives me the 开发者_Python百科exception

Exception in thread "Thread-2" java.lang.NullPointerException
 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
 at java.io.FilterOutputStream.close(FilterOutputStream.java:140)
 at FileDownloader.Download(FileDownloader.java:201)
 at FileDownloader$2.run(FileDownloader.java:114)
 at java.lang.Thread.run(Thread.java:637)

Could anyone tell me what the reason may be? and how i can solve this?

Thanks in advance

0

精彩评论

暂无评论...
验证码 换一张
取 消