在 MAC OSX 上从 url 下载文件?

发布于 2024-10-23 00:30:54 字数 2799 浏览 0 评论 0原文

AM 尝试使用 url & 从网络下载文件java中的urlconnection类使用此代码。

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();
                    }
                }
            }

当我在 Windows 系统上运行此代码时,它运行完美,但是当我将代码带到 mac os 时,它给了我一个异常

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)

有人能告诉我原因可能是什么吗?我该如何解决这个问题?

提前致谢

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文