在 MAC OSX 上从 url 下载文件?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论